复制代码 代码如下:
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();

      }

    }

标签:
XML,DataTable

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“.NET实现XML与DataTable互转的实例代码”

暂无“.NET实现XML与DataTable互转的实例代码”评论...