XML与DataSet的相互转换

XmlDatasetConvert 该类提供了四种方法:
1、将xml对象内容字符串转换为DataSet
2、将xml文件转换为DataSet
3、将DataSet转换为xml对象字符串
4、将DataSet转换为xml文件

复制代码
  1 //XmlDatasetConvert.cs
  2 
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Text;
  6 using System.Data;
  7 using System.IO;
  8 using System.Xml;
  9 
 10 namespace XmlDesign
 11 {
 12     class XmlDatasetConvert
 13     {
 14         //将xml对象内容字符串转换为DataSet
 15         public static DataSet ConvertXMLToDataSet(string xmlData)
 16         {
 17             StringReader stream = null;
 18             XmlTextReader reader = null;
 19             try
 20             {
 21                 DataSet xmlDS = new DataSet();
 22                 stream = new StringReader(xmlData);
 23                 //从stream装载到XmlTextReader
 24                 reader = new XmlTextReader(stream);
 25                 xmlDS.ReadXml(reader);
 26                 return xmlDS;
 27             }
 28             catch (System.Exception ex)
 29             {
 30                 throw ex;
 31             }
 32             finally
 33             {
 34                 if (reader != null) reader.Close();
 35             }
 36         }
 37 
 38         //将xml文件转换为DataSet
 39         public static DataSet ConvertXMLFileToDataSet(string xmlFile)
 40         {
 41             StringReader stream = null;
 42             XmlTextReader reader = null;
 43             try
 44             {
 45                 XmlDocument xmld = new XmlDocument();
 46                 xmld.Load(xmlFile);
 47 
 48                 DataSet xmlDS = new DataSet();
 49                 stream = new StringReader(xmld.InnerXml);
 50                 //从stream装载到XmlTextReader
 51                 reader = new XmlTextReader(stream);
 52                 xmlDS.ReadXml(reader);
 53                 //xmlDS.ReadXml(xmlFile);
 54                 return xmlDS;
 55             }
 56             catch (System.Exception ex)
 57             {
 58                 throw ex;
 59             }
 60             finally
 61             {
 62                 if (reader != null) reader.Close();
 63             }
 64         }
 65 
 66         //将DataSet转换为xml对象字符串
 67         public static string ConvertDataSetToXML(DataSet xmlDS)
 68         {
 69             MemoryStream stream = null;
 70             XmlTextWriter writer = null;
 71 
 72             try
 73             {
 74                 stream = new MemoryStream();
 75                 //从stream装载到XmlTextReader
 76                 writer = new XmlTextWriter(stream, Encoding.Unicode);
 77 
 78                 //用WriteXml方法写入文件.
 79                 xmlDS.WriteXml(writer);
 80                 int count = (int)stream.Length;
 81                 byte[] arr = new byte[count];
 82                 stream.Seek(0, SeekOrigin.Begin);
 83                 stream.Read(arr, 0, count);
 84 
 85                 UnicodeEncoding utf = new UnicodeEncoding();
 86                 return utf.GetString(arr).Trim();
 87             }
 88             catch (System.Exception ex)
 89             {
 90                 throw ex;
 91             }
 92             finally
 93             {
 94                 if (writer != null) writer.Close();
 95             }
 96         }
 97 
 98         //将DataSet转换为xml文件
 99         public static void ConvertDataSetToXMLFile(DataSet xmlDS,string xmlFile)
100         {
101             MemoryStream stream = null;
102             XmlTextWriter writer = null;
103 
104             try
105             {
106                 stream = new MemoryStream();
107                 //从stream装载到XmlTextReader
108                 writer = new XmlTextWriter(stream, Encoding.Unicode);
109 
110                 //用WriteXml方法写入文件.
111                 xmlDS.WriteXml(writer);
112                 int count = (int)stream.Length;
113                 byte[] arr = new byte[count];
114                 stream.Seek(0, SeekOrigin.Begin);
115                 stream.Read(arr, 0, count);
116 
117                 //返回Unicode编码的文本
118                 UnicodeEncoding utf = new UnicodeEncoding();
119                 StreamWriter sw = new StreamWriter(xmlFile);
120                 sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
121                 sw.WriteLine(utf.GetString(arr).Trim());
122                 sw.Close();
123             }
124             catch( System.Exception ex )
125             {
126                 throw ex;
127             }
128             finally
129             {
130                 if (writer != null) writer.Close();
131             }
132         }
133 
134     }
135 }
136 //使用示例
137 
138 using System;
139 using System.Collections.Generic;
140 using System.Text;
141 using System.Xml;
142 using System.Data;
143 
144 namespace XmlDesign
145 {
146     class Program
147     {
148         static void Main(string[] args)
149         {
150             DataSet ds = new DataSet();
151 
152             //转换一个XML文件(本地\网络均可)为一个DataSet
153             #region 转换一个XML文件(本地\网络均可)为一个DataSet
154             //http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss
155             //F:\study\001CSharp_Study\002Source\XmlDesign\XmlDesign\Save_Plan.xml
156             ds = XmlDatasetConvert.ConvertXMLFileToDataSet(@"c:\\adadsda1.xml");
157             Console.WriteLine("数据集名为\"{0}\",包含{1}个表", ds.DataSetName, ds.Tables.Count);
158             foreach(DataTable dt in ds.Tables)
159             {
160                 PrintTableName(dt.TableName);
161             };
162             #endregion
163 
164             //构造一个DataSet,并转换为XML字符串
165             #region 构造一个DataSet,并转换为XML字符串
166             DataSet ds1 = new DataSet();
167             DataTable dt1 = new DataTable();
168             dt1.TableName = "test";
169             dt1.Columns.Add("id");
170             dt1.Columns.Add("name");
171             dt1.Rows.Add("i001", "hekui");
172             dt1.Rows.Add("i002", "liyang");
173 
174             DataTable dt2 = new DataTable();
175             dt2.TableName = "test1";
176             dt2.Columns.Add("bookid");
177             dt2.Columns.Add("bookname");
178             dt2.Rows.Add("b001", "书本1");
179             dt2.Rows.Add("b002", "书本2");
180 
181             ds1.Tables.Add(dt1);
182             ds1.Tables.Add(dt2);
183             ds1.DataSetName = "方案";
184             string xmlOut = XmlDatasetConvert.ConvertDataSetToXML(ds1);
185             #endregion
186 
187             //转换一个XML字符串为一个DataSet
188             #region 转换一个XML字符串为一个DataSet
189             DataSet ds2 = new DataSet();
190             ds2 = XmlDatasetConvert.ConvertXMLToDataSet(xmlOut);
191             Console.WriteLine("数据集名为\"{0}\",包含{1}个表", ds2.DataSetName, ds2.Tables.Count);
192             foreach (DataTable dt in ds2.Tables)
193             {
194                 PrintTableName(dt.TableName);
195             };
196             #endregion
197 
198             //转换一个Dataset为一个XML文件
199             #region 转换一个Dataset为一个XML文件
200             XmlDatasetConvert.ConvertDataSetToXMLFile(ds2, "c:\\adadsda1.xml");
201             #endregion
202             
203             Console.ReadLine();
204         }
205 
206         private static void PrintTableName(string tableName)
207         {
208             Console.WriteLine(tableName);
209         }
210     }
211 }
posted @ 2019-09-15 11:18  net-sky  阅读(849)  评论(0编辑  收藏  举报