1、 XML 与 DataSet 的相互转换类送给大家一个 XML 与 DataSet 的相互转换的类:XmlDatasetConvert 该类提供了四种方法:1、将 xml 对象内容字符串转换为 DataSet2、将 xml 文件转换为 DataSet3、将 DataSet 转换为 xml 对象字符串4、将 DataSet 转换为 xml 文件XmlDatasetConvert.csusing System;using System.Collections.Generic;using System.Text;using System.Data;using System.IO;using Syst
2、em.Xml;namespace XmlDesignclass XmlDatasetConvert/将 xml 对象内容字符串转换为 DataSetpublic static DataSet ConvertXMLToDataSet(string xmlData)StringReader stream = null;XmlTextReader reader = null;tryDataSet xmlDS = new DataSet();stream = new StringReader(xmlData);/从 stream 装载到 XmlTextReaderreader = new XmlTex
3、tReader(stream);xmlDS.ReadXml(reader);return xmlDS;catch (System.Exception ex)throw ex;finallyif (reader != null) reader.Close();/将 xml 文件转换为 DataSetpublic static DataSet ConvertXMLFileToDataSet(string xmlFile)StringReader stream = null;XmlTextReader reader = null;tryXmlDocument xmld = new XmlDocume
4、nt();xmld.Load(xmlFile);DataSet xmlDS = new DataSet();stream = new StringReader(xmld.InnerXml);/从 stream 装载到 XmlTextReaderreader = new XmlTextReader(stream);xmlDS.ReadXml(reader);/xmlDS.ReadXml(xmlFile);return xmlDS;catch (System.Exception ex)throw ex;finallyif (reader != null) reader.Close();/将 Dat
5、aSet 转换为 xml 对象字符串public static string ConvertDataSetToXML(DataSet xmlDS)MemoryStream stream = null;XmlTextWriter writer = null;trystream = new MemoryStream();/从 stream 装载到 XmlTextReaderwriter = new XmlTextWriter(stream, Encoding.Unicode);/用 WriteXml 方法写入文件.xmlDS.WriteXml(writer);int count = (int)st
6、ream.Length;byte arr = new bytecount;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;finallyif (writer != null) writer.Close();/将 DataSet 转换为 xml 文件public static void ConvertD
7、ataSetToXMLFile(DataSet xmlDS,string xmlFile)MemoryStream stream = null;XmlTextWriter writer = null;trystream = new MemoryStream();/从 stream 装载到 XmlTextReaderwriter = new XmlTextWriter(stream, Encoding.Unicode);/用 WriteXml 方法写入文件.xmlDS.WriteXml(writer);int count = (int)stream.Length;byte arr = new b
8、ytecount;stream.Seek(0, SeekOrigin.Begin);stream.Read(arr, 0, count);/返回 Unicode 编码的文本UnicodeEncoding utf = new UnicodeEncoding();StreamWriter sw = new StreamWriter(xmlFile);sw.WriteLine(“);sw.WriteLine(utf.GetString(arr).Trim();sw.Close();catch( System.Exception ex )throw ex;finallyif (writer != nu
9、ll) writer.Close();使用示例using System;using System.Collections.Generic;using System.Text;using System.Xml;using System.Data;namespace XmlDesignclass Programstatic void Main(string args)DataSet ds = new DataSet();转换一个 XML 文件(本地/网络均可)为一个 DataSet#region 转换一个 XML 文件(本地/网络均可)为一个 DataSet/http:/ = XmlDataset
10、Convert.ConvertXMLFileToDataSet(“http:/ 个表“, ds.DataSetName, ds.Tables.Count);foreach(DataTable dt in ds.Tables)PrintTableName(dt.TableName);#endregion构造一个 DataSet,并转换为 XML 字符串#region 构造一个 DataSet,并转换为 XML 字符串DataSet ds1 = new DataSet();DataTable dt1 = new DataTable();dt1.TableName = “test“;dt1.Colu
11、mns.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.Ad
12、d(dt2);ds1.DataSetName = “方案“;string xmlOut = XmlDatasetConvert.ConvertDataSetToXML(ds1);#endregion转换一个 XML 字符串为一个 DataSet#region 转换一个 XML 字符串为一个 DataSetDataSet ds2 = new DataSet();ds2 = XmlDatasetConvert.ConvertXMLToDataSet(xmlOut);Console.WriteLine(“数据集名为/“0/“,包含1 个表“, ds2.DataSetName, ds2.Tables.
13、Count);foreach (DataTable dt in ds2.Tables)PrintTableName(dt.TableName);#endregion转换一个 Dataset 为一个 XML 文件#region 转换一个 Dataset 为一个 XML 文件XmlDatasetConvert.ConvertDataSetToXMLFile(ds2, “c:/adadsda1.xml“);#endregionConsole.ReadLine();private static void PrintTableName(string tableName)Console.WriteLine(tableName);