亲宝软件园·资讯

展开

.NET实现XML与DataTable互转 .NET实现XML与DataTable互转的实例代码

人气:0
想了解.NET实现XML与DataTable互转的实例代码的相关内容吗,在本文为您仔细讲解.NET实现XML与DataTable互转的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:XML,DataTable,下面大家一起来学习吧。

复制代码 代码如下:

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();

      }

    }

加载全部内容

相关教程
猜你喜欢
用户评论