XML與DataSet的相互轉換

轉:http://www.javashuo.com/article/p-osywiamd-ko.html

 XML與DataSet的相互轉換的類

1、XML與DataSet的相互轉換的類html

 

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

 

2、 該方法的使用示例網絡

 

 
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();   轉換一個XML文件(本地\\網絡都可)爲一個DataSet 構造一個DataSet,並轉換爲XML字符串   構造一個DataSet,並轉換爲XML字符串 轉換一個XML字符串爲一個DataSet   #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 轉換一個Dataset爲一個XML文件   #region 轉換一個Dataset爲一個XML文件  XmlDatasetConvert.ConvertDataSetToXMLFile(ds2, "c:\\\\adadsda1。xml");  #endregion   Console.ReadLine();  }   private static void PrintTableName(string tableName)  {  Console.WriteLine(tableName);  }  } }
相關文章
相關標籤/搜索