XML與DataSet的相互轉換

weixin_33766168發表於2017-11-03

XmlDatasetConvert 該類提供了四種方法: 

    1、將xml物件內容字串轉換為DataSet 
    2、將xml檔案轉換為DataSet 
    3、將DataSet轉換為xml物件字串 
    4、將DataSet轉換為xml檔案


XmlDatasetConvert.cs 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.IO;
  6. using System.Xml;
  7. namespace XmlDesign
  8. {
  9.     class XmlDatasetConvert
  10.     {
  11.         //將xml物件內容字串轉換為DataSet
  12.         public static DataSet ConvertXMLToDataSet(string xmlData)
  13.         {
  14.             StringReader stream = null;
  15.             XmlTextReader reader = null;
  16.             try
  17.             {
  18.                 DataSet xmlDS = new DataSet();
  19.                 stream = new StringReader(xmlData);
  20.                 //從stream裝載到XmlTextReader
  21.                 reader = new XmlTextReader(stream);
  22.                 xmlDS.ReadXml(reader);
  23.                 return xmlDS;
  24.             }
  25.             catch (System.Exception ex)
  26.             {
  27.                 throw ex;
  28.             }
  29.             finally
  30.             {
  31.                 if (reader != null)
  32.                     reader.Close();
  33.             }
  34.         }
  35.         //將xml檔案轉換為DataSet
  36.         public static DataSet ConvertXMLFileToDataSet(string xmlFile)
  37.         {
  38.             StringReader stream = null;
  39.             XmlTextReader reader = null;
  40.             try
  41.             {
  42.                 XmlDocument xmld = new XmlDocument();
  43.                 xmld.Load(xmlFile);
  44.                 DataSet xmlDS = new DataSet();
  45.                 stream = new StringReader(xmld.InnerXml);
  46.                 //從stream裝載到XmlTextReader
  47.                 reader = new XmlTextReader(stream);
  48.                 xmlDS.ReadXml(reader);
  49.                 //xmlDS.ReadXml(xmlFile);
  50.                 return xmlDS;
  51.             }
  52.             catch (System.Exception ex)
  53.             {
  54.                 throw ex;
  55.             }
  56.             finally
  57.             {
  58.                 if (reader != null)
  59.                     reader.Close();
  60.             }
  61.         }
  62.         //將DataSet轉換為xml物件字串
  63.         public static string ConvertDataSetToXML(DataSet xmlDS)
  64.         {
  65.             MemoryStream stream = null;
  66.             XmlTextWriter writer = null;
  67.             try
  68.             {
  69.                 stream = new MemoryStream();
  70.                 //從stream裝載到XmlTextReader
  71.                 writer = new XmlTextWriter(stream, Encoding.Unicode);
  72.                 //用WriteXml方法寫入檔案.
  73.                 xmlDS.WriteXml(writer);
  74.                 int count = (int) stream.Length;
  75.                 byte[] arr = new byte[count];
  76.                 stream.Seek(0, SeekOrigin.Begin);
  77.                 stream.Read(arr, 0, count);
  78.                 UnicodeEncoding utf = new UnicodeEncoding();
  79.                 return utf.GetString(arr).Trim();
  80.             }
  81.             catch (System.Exception ex)
  82.             {
  83.                 throw ex;
  84.             }
  85.             finally
  86.             {
  87.                 if (writer != null)
  88.                     writer.Close();
  89.             }
  90.         }
  91.         //將DataSet轉換為xml檔案
  92.         public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
  93.         {
  94.             MemoryStream stream = null;
  95.             XmlTextWriter writer = null;
  96.             try
  97.             {
  98.                 stream = new MemoryStream();
  99.                 //從stream裝載到XmlTextReader
  100.                 writer = new XmlTextWriter(stream, Encoding.Unicode);
  101.                 //用WriteXml方法寫入檔案.
  102.                 xmlDS.WriteXml(writer);
  103.                 int count = (int) stream.Length;
  104.                 byte[] arr = new byte[count];
  105.                 stream.Seek(0, SeekOrigin.Begin);
  106.                 stream.Read(arr, 0, count);
  107.                 //返回Unicode編碼的文字
  108.                 UnicodeEncoding utf = new UnicodeEncoding();
  109.                 StreamWriter sw = new StreamWriter(xmlFile);
  110.                 sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  111.                 sw.WriteLine(utf.GetString(arr).Trim());
  112.                 sw.Close();
  113.             }
  114.             catch (System.Exception ex)
  115.             {
  116.                 throw ex;
  117.             }
  118.             finally
  119.             {
  120.                 if (writer != null)
  121.                     writer.Close();
  122.             }
  123.         }
  124.     }
  125. }

 

使用例子

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml;
  5. using System.Data;
  6. namespace XmlDesign
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             DataSet ds = new DataSet();
  13.             #region 轉換一個XML檔案(本地\網路均可)為一個DataSet
  14.             //http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss
  15.             //F:\study\001CSharp_Study\002Source\XmlDesign\XmlDesign\Save_Plan.xml
  16.             ds = XmlDatasetConvert.ConvertXMLFileToDataSet(@"http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss");
  17.             Console.WriteLine("資料集名為\"{0}\",包含{1}個表", ds.DataSetName, ds.Tables.Count);
  18.             foreach (DataTable dt in ds.Tables)
  19.             {
  20.                 PrintTableName(dt.TableName);
  21.             }
  22.             #endregion
  23.             #region 構造一個DataSet,並轉換為XML字串
  24.             DataSet ds1 = new DataSet();
  25.             DataTable dt1 = new DataTable();
  26.             dt1.TableName = "test";
  27.             dt1.Columns.Add("id");
  28.             dt1.Columns.Add("name");
  29.             dt1.Rows.Add("i001""hekui");
  30.             dt1.Rows.Add("i002""liyang");
  31.             DataTable dt2 = new DataTable();
  32.             dt2.TableName = "test1";
  33.             dt2.Columns.Add("bookid");
  34.             dt2.Columns.Add("bookname");
  35.             dt2.Rows.Add("b001""書本1");
  36.             dt2.Rows.Add("b002""書本2");
  37.             ds1.Tables.Add(dt1);
  38.             ds1.Tables.Add(dt2);
  39.             ds1.DataSetName = "方案";
  40.             string xmlOut = XmlDatasetConvert.ConvertDataSetToXML(ds1);
  41.             #endregion
  42.             #region 轉換一個XML字串為一個DataSet
  43.             DataSet ds2 = new DataSet();
  44.             ds2 = XmlDatasetConvert.ConvertXMLToDataSet(xmlOut);
  45.             Console.WriteLine("資料集名為\"{0}\",包含{1}個表", ds2.DataSetName, ds2.Tables.Count);
  46.             foreach (DataTable dt in ds2.Tables)
  47.             {
  48.                 PrintTableName(dt.TableName);
  49.             }
  50.             #endregion
  51.             #region 轉換一個Dataset為一個XML檔案
  52.             XmlDatasetConvert.ConvertDataSetToXMLFile(ds2, "c:\\adadsda1.xml");
  53.             #endregion
  54.             Console.ReadLine();
  55.         }
  56.         private static void PrintTableName(string tableName)
  57.         {
  58.             Console.WriteLine(tableName);
  59.         }
  60.     }
  61. }



  62. 本文轉自94cool部落格園部落格,原文連結:http://www.cnblogs.com/94cool/archive/2011/07/21/2112920.html,如需轉載請自行聯絡原作者

相關文章