Dzwebs.Net

撰写电脑技术杂文十余年

XML与DataSet(DataTable)数据互转源代码示例

Admin | 2008-7-6 18:13:55 | 被阅次数 | 6790

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆
    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();
      }
    }

该杂文来自: 网站开发杂文

上一篇:xmlhttp异步操作之代码说明

下一篇:简单介绍XML在Web中的应用

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计