DataSet(DataTable)与XML互转

ZDNet软件频道 时间:2008-07-09 作者: | 中国IT实验室 我要评论()
本文关键词:DataTable DataSet XML 软件
DataSet(DataTable)与XML互转

 加引用:

   using System;
    using System.Data;
    using System.IO;
    using System.XML;
    using System.Text;
    // 相应C#代码:
    private string ConvertDataTableToXML(DataTable XMLDS)
    {
        MemoryStream stream = null;
        XMLTextWriter writer = null;
        try
        {
            stream = new MemoryStream();
            writer = new XMLTextWriter(stream, Encoding.Default);
            XMLDS.WriteXML(writer);
            int count = (int)stream.Length;
            byte[] arr = new byte[count];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(arr, 0, count);
            UTF8Encoding utf = new UTF8Encoding();
            return utf.GetString(arr).Trim();
        }
        catch
        {
            return String.Empty;
        }
        finally
        {
            if (writer != null) writer.Close();
        }
    }
    private DataSet ConvertXMLToDataSet(string XMLData)
    {
      StringReader stream = null;
      XMLTextReader reader = null;
      try
      {
        DataSet XMLDS = new DataSet();
        stream = new StringReader(XMLData);
        reader = new XMLTextReader(stream);
        XMLDS.ReadXML(reader);
        return XMLDS;
      }
      catch (Exception ex)
      {
        string strTest = ex.Message;
        return null;
      }
      finally
      {
        if (reader != null)
        reader.Close();
      }
    }

DataTable

DataSet

XML

软件


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134