XML与DataSet的相互转换的类

来源:互联网 发布:笔记本网卡mac地址修改 编辑:程序博客网 时间:2024/06/08 03:11
  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.   
  8. namespace XmlDesign  
  9. {  
  10.     class XmlDatasetConvert  
  11.     {  
  12.         //将xml对象内容字符串转换为DataSet  
  13.         public static DataSet ConvertXMLToDataSet(string xmlData)  
  14.         {  
  15.             StringReader stream = null;  
  16.             XmlTextReader reader = null;  
  17.             try  
  18.             {  
  19.                 DataSet xmlDS = new DataSet();  
  20.                 stream = new StringReader(xmlData);  
  21.                 //从stream装载到XmlTextReader  
  22.                 reader = new XmlTextReader(stream);  
  23.                 xmlDS.ReadXml(reader);  
  24.                 return xmlDS;  
  25.             }  
  26.             catch (System.Exception ex)  
  27.             {  
  28.                 throw ex;  
  29.             }  
  30.             finally  
  31.             {  
  32.                 if (reader != null) reader.Close();  
  33.             }  
  34.         }  
  35.   
  36.         //将xml文件转换为DataSet  
  37.         public static DataSet ConvertXMLFileToDataSet(string xmlFile)  
  38.         {  
  39.             StringReader stream = null;  
  40.             XmlTextReader reader = null;  
  41.             try  
  42.             {  
  43.                 XmlDocument xmld = new XmlDocument();  
  44.                 xmld.Load(xmlFile);  
  45.   
  46.                 DataSet xmlDS = new DataSet();  
  47.                 stream = new StringReader(xmld.InnerXml);  
  48.                 //从stream装载到XmlTextReader  
  49.                 reader = new XmlTextReader(stream);  
  50.                 xmlDS.ReadXml(reader);  
  51.                 //xmlDS.ReadXml(xmlFile);  
  52.                 return xmlDS;  
  53.             }  
  54.             catch (System.Exception ex)  
  55.             {  
  56.                 throw ex;  
  57.             }  
  58.             finally  
  59.             {  
  60.                 if (reader != null) reader.Close();  
  61.             }  
  62.         }  
  63.   
  64.         //将DataSet转换为xml对象字符串  
  65.         public static string ConvertDataSetToXML(DataSet xmlDS)  
  66.         {  
  67.             MemoryStream stream = null;  
  68.             XmlTextWriter writer = null;  
  69.   
  70.             try  
  71.             {  
  72.                 stream = new MemoryStream();  
  73.                 //从stream装载到XmlTextReader  
  74.                 writer = new XmlTextWriter(stream, Encoding.Unicode);  
  75.   
  76.                 //用WriteXml方法写入文件.  
  77.                 xmlDS.WriteXml(writer);  
  78.                 int count = (int)stream.Length;  
  79.                 byte[] arr = new byte[count];  
  80.                 stream.Seek(0, SeekOrigin.Begin);  
  81.                 stream.Read(arr, 0, count);  
  82.   
  83.                 UnicodeEncoding utf = new UnicodeEncoding();  
  84.                 return utf.GetString(arr).Trim();  
  85.             }  
  86.             catch (System.Exception ex)  
  87.             {  
  88.                 throw ex;  
  89.             }  
  90.             finally  
  91.             {  
  92.                 if (writer != null) writer.Close();  
  93.             }  
  94.         }  
  95.   
  96.         //将DataSet转换为xml文件  
  97.         public static void ConvertDataSetToXMLFile(DataSet xmlDS,string xmlFile)  
  98.         {  
  99.             MemoryStream stream = null;  
  100.             XmlTextWriter writer = null;  
  101.   
  102.             try  
  103.             {  
  104.                 stream = new MemoryStream();  
  105.                 //从stream装载到XmlTextReader  
  106.                 writer = new XmlTextWriter(stream, Encoding.Unicode);  
  107.   
  108.                 //用WriteXml方法写入文件.  
  109.                 xmlDS.WriteXml(writer);  
  110.                 int count = (int)stream.Length;  
  111.                 byte[] arr = new byte[count];  
  112.                 stream.Seek(0, SeekOrigin.Begin);  
  113.                 stream.Read(arr, 0, count);  
  114.   
  115.                 //返回Unicode编码的文本  
  116.                 UnicodeEncoding utf = new UnicodeEncoding();  
  117.                 StreamWriter sw = new StreamWriter(xmlFile);  
  118.                 sw.WriteLine("<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>");  
  119.                 sw.WriteLine(utf.GetString(arr).Trim());  
  120.                 sw.Close();  
  121.             }  
  122.             catch( System.Exception ex )  
  123.             {  
  124.                 throw ex;  
  125.             }  
  126.             finally  
  127.             {  
  128.                 if (writer != null) writer.Close();  
  129.             }  
  130.         }  
  131.   
  132.     }  
  133. }  
原创粉丝点击