惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler
I
InfoQ
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
The Cloudflare Blog
罗磊的独立博客

博客园 - 林中小轩

Linux笔记 用TreeView以递归显示选择磁盘上文件夹中全部文件夹和文件 WebBrowser实现:自动填充网页上的用户名和密码并点击登录按钮 C#调用免费天气预报WebService 使用C#调用PI-SDK进行基于PI的开发——使用PI-SDK建立与PI数据库的连接(转载) InfoPlus.21(一) - 林中小轩 - 博客园 Datagrid中弹出新窗体出现的含有[object]空白页面问题的解决 用VS.NET调试ASP页面 如何删除服务 - 林中小轩 - 博客园 史上最全word用法 一张舞女图测试你的左右脑切换能力【我看到左右都转呢~】 IP.21出现的错误 无法加载DLL(oci.dll) 假如你的年龄超过了23··· 如何经营婚姻 人生活的三种状态 前天晚上回到北京了 松口气了! 四叶草的祝福
XmlDatasetConvert类提供的四种方法
林中小轩 · 2009-12-08 · via 博客园 - 林中小轩

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();

            }

        }

    }

}