DataSet转换为xml文件 
    //将DataSet转换为xml文件
        private 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.Flush();
                sw.Close();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }

XML与DataSet的相互转换类

送给大家一个XML与DataSet的相互转换的类:
XmlDatasetConvert 该类提供了四种方法:
1、将xml对象内容字符串转换为DataSet
2、将xml文件转换为DataSet
3、将DataSet转换为xml对象字符串
4、将DataSet转换为xml文件

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

}
}

使用示例
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#region 转换一个XML文件(本地\网络均可)为一个DataSet
            //http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss
            //F:\study\001CSharp_Study\002Source\XmlDesign\XmlDesign\Save_Plan.xml
            ds = XmlDatasetConvert.ConvertXMLFileToDataSet(@"http://news.baidu.com/n?cmd=1&class=sportnews&tn=rss");
            Console.WriteLine("数据集名为\"{0}\",包含{1}个表", 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.Columns.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.Add(dt2);
            ds1.DataSetName = "方案";
            string xmlOut = XmlDatasetConvert.ConvertDataSetToXML(ds1);
            #endregion

转换一个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);
        }
    }
}

DataSet与Xml文件的互相转换的更多相关文章

  1. c#解析XML到DATASET及dataset转为xml文件函数

    //将xml对象内容字符串转换为DataSet         public static DataSet ConvertXMLToDataSet(string xmlData)         { ...

  2. C# datatable 与 xml文件之间的转换

    /// <summary> /// datatable转XML文件 /// </summary> /// <param name="dtTable"& ...

  3. C# 实现DataTable、DataSet与XML互相转换

    /**//// <summary> /// 把DataSet.DataTable.DataView格式转换成XML字符串.XML文件 /// </summary> public ...

  4. DataSet,DataTable,XML格式互转

    //// <summary> /// 将DataTable对象转换成XML字符串 /// </summary> /// <param name="dt" ...

  5. C#对象与XMl文件之间的相互转换

    C#提供三种序列化方式,分别为: 1.是使用BinaryFormatter进行串行化: 2.使用SoapFormatter进行串行化: 3.使用XmlSerializer进行串行化.其中对于Binar ...

  6. ASP.NET中把xml转为dataset与xml字符串转为dataset及dataset转为xml的代码

    转自:http://www.cnblogs.com/_zjl/archive/2011/04/08/2009087.html XmlDatasetConvert.csusing System;usin ...

  7. 简单实体类和xml文件的相互转换

    最近写一个题目,要求将一组员工实体类转换成xml文件,或将xml文件转换成一组实体类.题目不难,但写完感觉可以利用泛型和反射将任意一个实体类和xml文件进行转换.于是今天下午立马动手 试了下,做了个简 ...

  8. PLSQL解析XML文件

     参考网上资料学习汇总 在PL/SQL中利用XML ,Oracle提供了几个组件,让开发人员能轻松地利用XML技术.这些组件包括: 1.  XML 分析程序.即用来分析.构造和验证XML文档.. ...

  9. C#对象与XMl文件之间的相互转换(转)

    本文是对C#中对象与XMl文件之间的相互转换进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 C#提供三种序列化方式,分别为:1.是使用BinaryFormatter进行串行化: 2.使 ...

随机推荐

  1. CodeForces 705A(训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/A 从第三个输出中可看出规律, I hate that I love that I hate it ...

  2. pthread clean up

    https://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part4/ http://www.cnblogs.com/xfi ...

  3. Visual Studio Online Integrations-Testing

    原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs

  4. jquery json ajax -2

    如果使用的是虚拟空间, 那么你不能操纵/配置服务器上的php.ini配置文件 但是可以在自己的php文件中, 通过ini_set(...), 和一些对应的函数, 去重置(临时的,仅在当前文件中生效的) ...

  5. 模式串匹配之KMP算法

    模式串匹配之KMP算法 KMP算法 模式值计算(next[j]) (1) next[0]=-1,  第一个字符模式值为-1 (2) next[j]=-1, T中下标为j的字符与首字符相同,且j前面的1 ...

  6. 并发包之Future:代码级控制超时时间

    先谢Doug Lea. 使用场景: 最近在做webservice调用的时候,发现一个问题,对方的webservice接口很不稳定,所以在获取的数据时候经常要等待很久才能把数据全部拉回来,甚至有时候直接 ...

  7. Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇——开发工具库篇,**主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容 ...

  8. js检测是否安装了flash插件

    function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 var isIE = ...

  9. static_cast dynamic_cast const_cast reinterpret_cast总结对比

    [本文链接] http://www.cnblogs.com/hellogiser/p/static_cast-dynamic_cast-const_cast-reinterpret_cast.html ...

  10. 【转】Apache Solr 访问权限控制

    本文转自:http://www.cnblogs.com/ibook360/archive/2011/11/07/2239247.html 在Tomcat6增加 Solr的访问权限方法如下: 编辑tom ...