程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> .NET實現XML與DataTable互轉的實例代碼

.NET實現XML與DataTable互轉的實例代碼

編輯:ASP.NET基礎

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

      }

    }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved