XML與DataSet的相互轉換
XmlDatasetConvert 該類提供了四種方法:
1、將xml物件內容字串轉換為DataSet
2、將xml檔案轉換為DataSet
3、將DataSet轉換為xml物件字串
4、將DataSet轉換為xml檔案
XmlDatasetConvert.cs
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.IO;
- using System.Xml;
- namespace XmlDesign
- {
- class XmlDatasetConvert
- {
- //將xml物件內容字串轉換為DataSet
- public static DataSet ConvertXMLToDataSet(string xmlData)
- {
- StringReader stream = null;
- XmlTextReader reader = null;
- try
- {
- DataSet xmlDS = new DataSet();
- stream = new StringReader(xmlData);
- //從stream裝載到XmlTextReader
- reader = new XmlTextReader(stream);
- xmlDS.ReadXml(reader);
- return xmlDS;
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (reader != null)
- reader.Close();
- }
- }
- //將xml檔案轉換為DataSet
- public static DataSet ConvertXMLFileToDataSet(string xmlFile)
- {
- StringReader stream = null;
- XmlTextReader reader = null;
- try
- {
- XmlDocument xmld = new XmlDocument();
- xmld.Load(xmlFile);
- DataSet xmlDS = new DataSet();
- stream = new StringReader(xmld.InnerXml);
- //從stream裝載到XmlTextReader
- reader = new XmlTextReader(stream);
- xmlDS.ReadXml(reader);
- //xmlDS.ReadXml(xmlFile);
- return xmlDS;
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (reader != null)
- reader.Close();
- }
- }
- //將DataSet轉換為xml物件字串
- public static string ConvertDataSetToXML(DataSet xmlDS)
- {
- MemoryStream stream = null;
- XmlTextWriter writer = null;
- try
- {
- stream = new MemoryStream();
- //從stream裝載到XmlTextReader
- writer = new XmlTextWriter(stream, Encoding.Unicode);
- //用WriteXml方法寫入檔案.
- xmlDS.WriteXml(writer);
- int count = (int) stream.Length;
- byte[] arr = new byte[count];
- stream.Seek(0, SeekOrigin.Begin);
- stream.Read(arr, 0, count);
- UnicodeEncoding utf = new UnicodeEncoding();
- return utf.GetString(arr).Trim();
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (writer != null)
- writer.Close();
- }
- }
- //將DataSet轉換為xml檔案
- public static void ConvertDataSetToXMLFile(DataSet xmlDS, string xmlFile)
- {
- MemoryStream stream = null;
- XmlTextWriter writer = null;
- try
- {
- stream = new MemoryStream();
- //從stream裝載到XmlTextReader
- writer = new XmlTextWriter(stream, Encoding.Unicode);
- //用WriteXml方法寫入檔案.
- xmlDS.WriteXml(writer);
- int count = (int) stream.Length;
- byte[] arr = new byte[count];
- stream.Seek(0, SeekOrigin.Begin);
- stream.Read(arr, 0, count);
- //返回Unicode編碼的文字
- UnicodeEncoding utf = new UnicodeEncoding();
- StreamWriter sw = new StreamWriter(xmlFile);
- sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
- sw.WriteLine(utf.GetString(arr).Trim());
- sw.Close();
- }
- catch (System.Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (writer != null)
- writer.Close();
- }
- }
- }
- }
使用例子
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Xml;
- using System.Data;
- namespace XmlDesign
- {
- class Program
- {
- static void Main(string[] args)
- {
- DataSet ds = new DataSet();
- #region 轉換一個XML檔案(本地\網路均可)為一個DataSet
- //http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss
- //F:\study\001CSharp_Study\002Source\XmlDesign\XmlDesign\Save_Plan.xml
- ds = XmlDatasetConvert.ConvertXMLFileToDataSet(@"http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss");
- Console.WriteLine("資料集名為\"{0}\",包含{1}個表", ds.DataSetName, ds.Tables.Count);
- foreach (DataTable dt in ds.Tables)
- {
- PrintTableName(dt.TableName);
- }
- #endregion
- #region 構造一個DataSet,並轉換為XML字串
- DataSet ds1 = new DataSet();
- DataTable dt1 = new DataTable();
- dt1.TableName = "test";
- dt1.Columns.Add("id");
- dt1.Columns.Add("name");
- dt1.Rows.Add("i001", "hekui");
- dt1.Rows.Add("i002", "liyang");
- DataTable dt2 = new DataTable();
- dt2.TableName = "test1";
- dt2.Columns.Add("bookid");
- dt2.Columns.Add("bookname");
- dt2.Rows.Add("b001", "書本1");
- dt2.Rows.Add("b002", "書本2");
- ds1.Tables.Add(dt1);
- ds1.Tables.Add(dt2);
- ds1.DataSetName = "方案";
- string xmlOut = XmlDatasetConvert.ConvertDataSetToXML(ds1);
- #endregion
- #region 轉換一個XML字串為一個DataSet
- DataSet ds2 = new DataSet();
- ds2 = XmlDatasetConvert.ConvertXMLToDataSet(xmlOut);
- Console.WriteLine("資料集名為\"{0}\",包含{1}個表", ds2.DataSetName, ds2.Tables.Count);
- foreach (DataTable dt in ds2.Tables)
- {
- PrintTableName(dt.TableName);
- }
- #endregion
- #region 轉換一個Dataset為一個XML檔案
- XmlDatasetConvert.ConvertDataSetToXMLFile(ds2, "c:\\adadsda1.xml");
- #endregion
- Console.ReadLine();
- }
- private static void PrintTableName(string tableName)
- {
- Console.WriteLine(tableName);
- }
- }
- }
- 本文轉自94cool部落格園部落格,原文連結:http://www.cnblogs.com/94cool/archive/2011/07/21/2112920.html,如需轉載請自行聯絡原作者
相關文章
- java 物件與xml相互轉換Java物件XML
- xml與陣列的相互轉換——phpXML陣列PHP
- 通過JDOM實現XML與String的相互轉換XML
- UIImage與Iplimage相互轉換UI
- SDOM與QDOM相互轉換
- DataTable與List相互轉換
- ascii碼與字元的相互轉換ASCII字元
- DataSet與泛型集合間的互相轉換泛型
- DOM物件與jquery物件的相互轉換物件jQuery
- jQuery物件與Dom物件的相互轉換jQuery物件
- 陣列與字串方法與相互轉換陣列字串
- 旋轉矩陣與尤拉角的相互轉換矩陣
- Word文件與WPS文件的相互轉換(轉)
- string與數字相互轉換
- JSON字串與HashMap相互轉換JSON字串HashMap
- java 字串與檔案相互轉換Java字串
- pandas中dataframe與dict相互轉換
- spark: RDD與DataFrame之間的相互轉換Spark
- Word中文字與表格的相互轉換
- 轉:java中陣列與List相互轉換的方法Java陣列
- string與char陣列相互轉換陣列
- jQuery 物件 與 原生 DOM 物件 相互轉換jQuery物件
- js時間戳與日期格式的相互轉換JS時間戳
- 簡單易用的JSON與List相互轉換JSON
- java物件與json物件間的相互轉換Java物件JSON
- mysql時間與字串之間相互轉換MySql字串
- 塊級元素與內聯元素相互轉換
- JavaScript陣列與字串相互轉換 join、splitJavaScript陣列字串
- Python字典格式與JSON格式的相互轉換PythonJSON
- C語言中字串與整數的相互轉換C語言字串
- C/C++:enum的理解 / enum與int的相互轉換C++
- C 語言整數與字串的相互轉換介紹字串
- C# byte陣列與Image相互轉換的方法C#陣列
- RVA和RAW相互轉換
- 陣列和集合的相互轉換陣列
- 字串和陣列的相互轉換字串陣列
- SCN 時間戳的相互轉換時間戳
- 字串和數字的相互轉換字串