XML与DataSet的相互转换

XML与DataSet的相互转换

转:https://www.cnblogs.com/kunEssay/p/6168824.html

XML与DataSet的相互转换的类

一、XML与DataSet的相互转换的类

using System;
XML与DataSet的相互转换-LMLPHP

using System.Collections.Generic;

XML与DataSet的相互转换-LMLPHP

using System.Text;

XML与DataSet的相互转换-LMLPHP

using System.Data;

XML与DataSet的相互转换-LMLPHP

using System.IO;

XML与DataSet的相互转换-LMLPHP

using System.Xml;

XML与DataSet的相互转换-LMLPHPXML与DataSet的相互转换-LMLPHP

namespace XmlDesign

XML与DataSet的相互转换-LMLPHP

{
XML与DataSet的相互转换-LMLPHP class XmlDatasetConvert
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP //将xml对象内容字符串转换为DataSet
XML与DataSet的相互转换-LMLPHP public static DataSet ConvertXMLToDataSet(string xmlData)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP StringReader stream = null;
XML与DataSet的相互转换-LMLPHP XmlTextReader reader = null;
XML与DataSet的相互转换-LMLPHP try
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP DataSet xmlDS = new DataSet();
XML与DataSet的相互转换-LMLPHP stream = new StringReader(xmlData);
XML与DataSet的相互转换-LMLPHP //从stream装载到XmlTextReader
XML与DataSet的相互转换-LMLPHP reader = new XmlTextReader(stream);
XML与DataSet的相互转换-LMLPHP xmlDS.ReadXml(reader);
XML与DataSet的相互转换-LMLPHP return xmlDS;
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP catch (System.Exception ex)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP throw ex;
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP finally
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP if (reader != null) reader.Close();
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP //将xml文件转换为DataSet
XML与DataSet的相互转换-LMLPHP public static DataSet ConvertXMLFileToDataSet(string xmlFile)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP StringReader stream = null;
XML与DataSet的相互转换-LMLPHP XmlTextReader reader = null;
XML与DataSet的相互转换-LMLPHP try
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP XmlDocument xmld = new XmlDocument();
XML与DataSet的相互转换-LMLPHP xmld.Load(xmlFile);
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP DataSet xmlDS = new DataSet();
XML与DataSet的相互转换-LMLPHP stream = new StringReader(xmld.InnerXml);
XML与DataSet的相互转换-LMLPHP //从stream装载到XmlTextReader
XML与DataSet的相互转换-LMLPHP reader = new XmlTextReader(stream);
XML与DataSet的相互转换-LMLPHP xmlDS.ReadXml(reader);
XML与DataSet的相互转换-LMLPHP //xmlDS.ReadXml(xmlFile);
XML与DataSet的相互转换-LMLPHP return xmlDS;
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP catch (System.Exception ex)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP throw ex;
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP finally
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP if (reader != null) reader.Close();
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP //将DataSet转换为xml对象字符串
XML与DataSet的相互转换-LMLPHP public static string ConvertDataSetToXML(DataSet xmlDS)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP MemoryStream stream = null;
XML与DataSet的相互转换-LMLPHP XmlTextWriter writer = null;
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP try
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP stream = new MemoryStream();
XML与DataSet的相互转换-LMLPHP //从stream装载到XmlTextReader
XML与DataSet的相互转换-LMLPHP writer = new XmlTextWriter(stream, Encoding.Unicode);
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP //用WriteXml方法写入文件.
XML与DataSet的相互转换-LMLPHP xmlDS.WriteXml(writer);
XML与DataSet的相互转换-LMLPHP int count = (int)stream.Length;
XML与DataSet的相互转换-LMLPHP byte[] arr = new byte[count];
XML与DataSet的相互转换-LMLPHP stream.Seek(0, SeekOrigin.Begin);
XML与DataSet的相互转换-LMLPHP stream.Read(arr, 0, count);
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP UnicodeEncoding utf = new UnicodeEncoding();
XML与DataSet的相互转换-LMLPHP return utf.GetString(arr).Trim();
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP catch (System.Exception ex)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP throw ex;
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP finally
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP if (writer != null) writer.Close();
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP //将DataSet转换为xml文件
XML与DataSet的相互转换-LMLPHP public static void ConvertDataSetToXMLFile(DataSet xmlDS,string xmlFile)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP MemoryStream stream = null;
XML与DataSet的相互转换-LMLPHP XmlTextWriter writer = null;
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP try
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP stream = new MemoryStream();
XML与DataSet的相互转换-LMLPHP //从stream装载到XmlTextReader
XML与DataSet的相互转换-LMLPHP writer = new XmlTextWriter(stream, Encoding.Unicode);
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP //用WriteXml方法写入文件.
XML与DataSet的相互转换-LMLPHP xmlDS.WriteXml(writer);
XML与DataSet的相互转换-LMLPHP int count = (int)stream.Length;
XML与DataSet的相互转换-LMLPHP byte[] arr = new byte[count];
XML与DataSet的相互转换-LMLPHP stream.Seek(0, SeekOrigin.Begin);
XML与DataSet的相互转换-LMLPHP stream.Read(arr, 0, count);
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP //返回Unicode编码的文本
XML与DataSet的相互转换-LMLPHP UnicodeEncoding utf = new UnicodeEncoding();
XML与DataSet的相互转换-LMLPHP StreamWriter sw = new StreamWriter(xmlFile);
XML与DataSet的相互转换-LMLPHP sw.WriteLine("<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>");
XML与DataSet的相互转换-LMLPHP sw.WriteLine(utf.GetString(arr).Trim());
XML与DataSet的相互转换-LMLPHP sw.Close();
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP catch( System.Exception ex )
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP throw ex;
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP finally
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP if (writer != null) writer.Close();
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP}
XML与DataSet的相互转换-LMLPHP

二、 该方法的使用示例

 
XML与DataSet的相互转换-LMLPHPXML与DataSet的相互转换-LMLPHP

using System;

XML与DataSet的相互转换-LMLPHP

using System.Collections.Generic;

XML与DataSet的相互转换-LMLPHP

using System.Text;

XML与DataSet的相互转换-LMLPHP

using System.Xml;

XML与DataSet的相互转换-LMLPHP

using System.Data;

XML与DataSet的相互转换-LMLPHPXML与DataSet的相互转换-LMLPHP

namespace XmlDesign

XML与DataSet的相互转换-LMLPHP

{
XML与DataSet的相互转换-LMLPHP class Program
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP static void Main(string[] args)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP DataSet ds = new DataSet();
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP 转换一个XML文件(本地\\网络均可)为一个DataSet 构造一个DataSet,并转换为XML字符串
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP 构造一个DataSet,并转换为XML字符串 转换一个XML字符串为一个DataSet
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP #region 转换一个XML字符串为一个DataSet
XML与DataSet的相互转换-LMLPHP DataSet ds2 = new DataSet();
XML与DataSet的相互转换-LMLPHP ds2 = XmlDatasetConvert.ConvertXMLToDataSet(xmlOut);
XML与DataSet的相互转换-LMLPHP Console.WriteLine("数据集名为\\"{0}\\",包含{1}个表",
XML与DataSet的相互转换-LMLPHP ds2.DataSetName, ds2.Tables.Count);
XML与DataSet的相互转换-LMLPHP foreach (DataTable dt in ds2.Tables)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP PrintTableName(dt.TableName);
XML与DataSet的相互转换-LMLPHP };
XML与DataSet的相互转换-LMLPHP #endregion 转换一个Dataset为一个XML文件
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP #region 转换一个Dataset为一个XML文件
XML与DataSet的相互转换-LMLPHP XmlDatasetConvert.ConvertDataSetToXMLFile(ds2, "c:\\\\adadsda1。xml");
XML与DataSet的相互转换-LMLPHP #endregion
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP Console.ReadLine();
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP
XML与DataSet的相互转换-LMLPHP private static void PrintTableName(string tableName)
XML与DataSet的相互转换-LMLPHP {
XML与DataSet的相互转换-LMLPHP Console.WriteLine(tableName);
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP }
XML与DataSet的相互转换-LMLPHP}
05-08 08:07