// <summary>  
    /// XML形式的字符串、XML文江转换成DataSet、DataTable格式  
    /// </summary>  
    public class XmlToData  
    {  
        /**//// <summary>  
        /// 将Xml内容字符串转换成DataSet对象  
        /// </summary>  
        /// <param name="xmlStr">Xml内容字符串</param>  
        /// <returns>DataSet对象</returns>  
        public static DataSet CXmlToDataSet(string xmlStr)  
        {  
            if (!string.IsNullOrEmpty(xmlStr))  
            {  
                StringReader StrStream = null;  
                XmlTextReader Xmlrdr = null;  
                try  
                {  
                    DataSet ds = new DataSet();  
                    //读取字符串中的信息  
                    StrStream = new StringReader(xmlStr);  
                    //获取StrStream中的数据  
                    Xmlrdr = new XmlTextReader(StrStream);  
                    //ds获取Xmlrdr中的数据                  
                    ds.ReadXml(Xmlrdr);  
                    return ds;  
                }  
                catch (Exception e)  
                {  
                    throw e;  
                }  
                finally  
                {  
                    //释放资源  
                    if (Xmlrdr != null)  
                    {  
                        Xmlrdr.Close();  
                        StrStream.Close();  
                        StrStream.Dispose();  
                    }  
                }  
            }  
            else  
            {  
                return null;  
            }  
        }  
        /**//// <summary>  
        /// 将Xml字符串转换成DataTable对象  
        /// </summary>  
        /// <param name="xmlStr">Xml字符串</param>  
        /// <param name="tableIndex">Table表索引</param>  
        /// <returns>DataTable对象</returns>  
        public static DataTable CXmlToDatatTable(string xmlStr, int tableIndex)  
        {  
            return CXmlToDataSet(xmlStr).Tables[tableIndex];  
        }  
        /**//// <summary>  
        /// 将Xml字符串转换成DataTable对象  
        /// </summary>  
        /// <param name="xmlStr">Xml字符串</param>  
        /// <returns>DataTable对象</returns>  
        public static DataTable CXmlToDatatTable(string xmlStr)  
        {  
            return CXmlToDataSet(xmlStr).Tables[0];  
        }  
        /**//// <summary>  
        /// 读取Xml文件信息,并转换成DataSet对象  
        /// </summary>  
        /// <remarks>  
        /// DataSet ds = new DataSet();  
        /// ds = CXmlFileToDataSet("/XML/upload.xml");  
        /// </remarks>  
        /// <param name="xmlFilePath">Xml文件地址</param>  
        /// <returns>DataSet对象</returns>  
        public static DataSet CXmlFileToDataSet(string xmlFilePath)  
        {  
            if (!string.IsNullOrEmpty(xmlFilePath))  
            {  
                string path = HttpContext.Current.Server.MapPath(xmlFilePath);  
                StringReader StrStream = null;  
                XmlTextReader Xmlrdr = null;  
                try  
                {  
                    XmlDocument xmldoc = new XmlDocument();  
                    //根据地址加载Xml文件  
                    xmldoc.Load(path);  
  
                    DataSet ds = new DataSet();  
                    //读取文件中的字符流  
                    StrStream = new StringReader(xmldoc.InnerXml);  
                    //获取StrStream中的数据  
                    Xmlrdr = new XmlTextReader(StrStream);  
                    //ds获取Xmlrdr中的数据  
                    ds.ReadXml(Xmlrdr);  
                    return ds;  
                }  
                catch (Exception e)  
                {  
                    throw e;  
                }  
                finally  
                {  
                    //释放资源  
                    if (Xmlrdr != null)  
                    {  
                        Xmlrdr.Close();  
                        StrStream.Close();  
                        StrStream.Dispose();  
                    }  
                }  
            }  
            else  
            {  
                return null;  
            }  
        }  
        /**//// <summary>  
        /// 读取Xml文件信息,并转换成DataTable对象  
        /// </summary>  
        /// <param name="xmlFilePath">xml文江路径</param>  
        /// <param name="tableIndex">Table索引</param>  
        /// <returns>DataTable对象</returns>  
        public static DataTable CXmlToDataTable(string xmlFilePath, int tableIndex)  
        {  
            return CXmlFileToDataSet(xmlFilePath).Tables[tableIndex];  
        }  
        /**//// <summary>  
        /// 读取Xml文件信息,并转换成DataTable对象  
        /// </summary>  
        /// <param name="xmlFilePath">xml文江路径</param>  
        /// <returns>DataTable对象</returns>  
        public static DataTable CXmlToDataTable(string xmlFilePath)  
        {  
            return CXmlFileToDataSet(xmlFilePath).Tables[0];  
        }  
    }

/// <summary>  
    /// 把DataSet、DataTable、DataView格式转换成XML字符串、XML文件  
    /// </summary>  
    public class DataToXml
    {
        /**/
        /// <summary>  
        /// 将DataTable对象转换成XML字符串  
        /// </summary>  
        /// <param name="dt">DataTable对象</param>  
        /// <returns>XML字符串</returns>  
        public static string CDataToXml(DataTable dt)
        {
            if (dt != null)
            {
                MemoryStream ms = null;
                XmlTextWriter XmlWt = null;
                try
                {
                    ms = new MemoryStream();
                    //根据ms实例化XmlWt  
                    XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
                    //获取ds中的数据  
                    dt.WriteXml(XmlWt);
                    int count = (int)ms.Length;
                    byte[] temp = new byte[count];
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.Read(temp, 0, count);
                    //返回Unicode编码的文本  
                    UnicodeEncoding ucode = new UnicodeEncoding();
                    string returnValue = ucode.GetString(temp).Trim();
                    return returnValue;
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    //释放资源  
                    if (XmlWt != null)
                    {
                        XmlWt.Close();
                        ms.Close();
                        ms.Dispose();
                    }
                }
            }
            else
            {
                return "";
            }
        }
        /**/
        /// <summary>  
        /// 将DataSet对象中指定的Table转换成XML字符串  
        /// </summary>  
        /// <param name="ds">DataSet对象</param>  
        /// <param name="tableIndex">DataSet对象中的Table索引</param>  
        /// <returns>XML字符串</returns>  
        public static string CDataToXml(DataSet ds, int tableIndex)
        {
            if (tableIndex != -1)
            {
                return CDataToXml(ds.Tables[tableIndex]);
            }
            else
            {
                return CDataToXml(ds.Tables[0]);
            }
        }
        /**/
        /// <summary>  
        /// 将DataSet对象转换成XML字符串  
        /// </summary>  
        /// <param name="ds">DataSet对象</param>  
        /// <returns>XML字符串</returns>  
        public static string CDataToXml(DataSet ds)
        {
            return CDataToXml(ds, -1);
        }
        /**/
        /// <summary>  
        /// 将DataView对象转换成XML字符串  
        /// </summary>  
        /// <param name="dv">DataView对象</param>  
        /// <returns>XML字符串</returns>  
        public static string CDataToXml(DataView dv)
        {
            return CDataToXml(dv.Table);
        }

/**/
        /// <summary>  
        /// 将DataSet对象数据保存为XML文件  
        /// </summary>  
        /// <param name="dt">DataSet</param>  
        /// <param name="xmlFilePath">XML文件路径</param>  
        /// <returns>bool值</returns>  
        public static bool CDataToXmlFile(DataTable dt, string xmlFilePath)  
        {  
            if ((dt != null) && (!string.IsNullOrEmpty(xmlFilePath)))  
            {  
                string path = HttpContext.Current.Server.MapPath(xmlFilePath);  
                MemoryStream ms = null;  
                XmlTextWriter XmlWt = null;  
                try  
                {  
                    ms = new MemoryStream();  
                    //根据ms实例化XmlWt  
                    XmlWt = new XmlTextWriter(ms, Encoding.Unicode);  
                    //获取ds中的数据  
                    dt.WriteXml(XmlWt);  
                    int count = (int)ms.Length;  
                    byte[] temp = new byte[count];  
                    ms.Seek(0, SeekOrigin.Begin);  
                    ms.Read(temp, 0, count);  
                    //返回Unicode编码的文本  
                    UnicodeEncoding ucode = new UnicodeEncoding();  
                    //写文件  
                    StreamWriter sw = new StreamWriter(path);  
                    sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");  
                    sw.WriteLine(ucode.GetString(temp).Trim());  
                    sw.Close();  
                    return true;  
                }  
                catch (System.Exception ex)  
                {  
                    throw ex;  
                }  
                finally  
                {  
                    //释放资源  
                    if (XmlWt != null)  
                    {  
                        XmlWt.Close();  
                        ms.Close();  
                        ms.Dispose();  
                    }  
                }  
            }  
            else  
            {  
                return false;  
            }  
        }
        /**/
        /// <summary>  
        /// 将DataSet对象中指定的Table转换成XML文件  
        /// </summary>  
        /// <param name="ds">DataSet对象</param>  
        /// <param name="tableIndex">DataSet对象中的Table索引</param>  
        /// <param name="xmlFilePath">xml文件路径</param>  
        /// <returns>bool]值</returns>  
        public static bool CDataToXmlFile(DataSet ds, int tableIndex, string xmlFilePath)
        {
            if (tableIndex != -1)
            {
                return CDataToXmlFile(ds.Tables[tableIndex], xmlFilePath);
            }
            else
            {
                return CDataToXmlFile(ds.Tables[0], xmlFilePath);
            }
        }
        /**/
        /// <summary>  
        /// 将DataSet对象转换成XML文件  
        /// </summary>  
        /// <param name="ds">DataSet对象</param>  
        /// <param name="xmlFilePath">xml文件路径</param>  
        /// <returns>bool]值</returns>  
        public static bool CDataToXmlFile(DataSet ds, string xmlFilePath)
        {
            return CDataToXmlFile(ds, -1, xmlFilePath);
        }
        /**/
        /// <summary>  
        /// 将DataView对象转换成XML文件  
        /// </summary>  
        /// <param name="dv">DataView对象</param>  
        /// <param name="xmlFilePath">xml文件路径</param>  
        /// <returns>bool]值</returns>  
        public static bool CDataToXmlFile(DataView dv, string xmlFilePath)
        {
            return CDataToXmlFile(dv.Table, xmlFilePath);
        }
    }

//C#代码

private string ConvertDataTableToXML(DataTable xmlDS)
        {
            MemoryStream stream = null;
            XmlTextWriter writer = null;
            try
            {
                stream = new MemoryStream();
                writer = new XmlTextWriter(stream, Encoding.Default);
                xmlDS.WriteXml(writer);
                int count = (int)stream.Length;
                byte[] arr = new byte[count];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(arr, 0, count);
                UTF8Encoding utf = new UTF8Encoding();
                return utf.GetString(arr).Trim();
            }
            catch
            {
                return String.Empty;
            }
            finally
            {
                if (writer != null) writer.Close();
            }
        }

private DataSet ConvertXMLToDataSet(string xmlData)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmlData);
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                return xmlDS;
            }
            catch (Exception ex)
            {
                string strTest = ex.Message;
                return null;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }

XML 与 DataSet/DataTable 互相转换实例(C#)——转载的更多相关文章

  1. DataSet,DataTable,XML格式互转

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

  2. DataSet & DataTable &DataRow 深入浅出

    本篇文章适合有一定的基础的人去查看 ,最好学习过一定net 编程基础在来查看此文章. 1.概念 DataSet是ADO.NET的中心概念.可以把DataSet当成内存中的数据库,DataSet是不依赖 ...

  3. XML与DataSet的相互转换

    转:https://www.cnblogs.com/kunEssay/p/6168824.html XML与DataSet的相互转换的类 一.XML与DataSet的相互转换的类 using Syst ...

  4. XML与DataSet的相互转换的类

    一.XML与DataSet的相互转换的类 using System; using System.Collections.Generic; using System.Text; using System ...

  5. C# DataSet与DataTable的区别和用法 ---转载

    C# DataSet与DataTable的区别和用法 转载:https://www.cnblogs.com/liuyi-li/p/6340411.html DataSet是数据集,DataTable是 ...

  6. 转换实例存储支持为EBS支持的AMI

    转换实例存储支持为EBS支持的AMI 注:不能将实例存储支持的Windows AMI 转换为 EBS 支持的 AMI.并且,你只能转换你所拥有的 AMI. 1. 从一个EBS支持的AMI启动一个Ama ...

  7. C#使用DataSet Datatable更新数据库的三种实现方法

    本文以实例形式讲述了使用DataSet Datatable更新数据库的三种实现方法,包括CommandBuilder 方法.DataAdapter 更新数据源以及使用sql语句更新.分享给大家供大家参 ...

  8. XML与DataSet相互转换,DataSet查询

    以FileShare.Read形式读XML文件: string hotspotXmlStr = string.Empty; try { Stream fileStream = new FileStre ...

  9. [Json] C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json (转载)

    点击下载 ConvertJson.rar 本类实现了 C#ConvertJson|List转成Json|对象|集合|DataSet|DataTable|DataReader转成Json|等功能大家先预 ...

随机推荐

  1. Pixar Shorts 皮克斯动画短片全集

    [原创短片](Theatrical Shorts)16部 <安德鲁和威利冒险记><顽皮跳跳灯><红色的梦><锡铁小兵><小雪人大行动>< ...

  2. vim operation

    note:  转自 www.quora.com ,很好的网站. 具体链接如下: https://www.quora.com/What-are-some-impressive-demos-of-Vim- ...

  3. CSS Bugs 解决方案

    说明:本文档兼容性测试基础环境为:windows系统:IE6-IE10, Firefox6.0, Chrome13.0, Safari5.1, Opera11.51 Bugs及解决方案列表(以下实例默 ...

  4. service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误

    service mysql start出错,mysql启动不了,解决mysql: unrecognized service错误的方法如下: [root@ctohome.com ~]# service ...

  5. NLP Deep Learning

    http://blog.csdn.net/itplus http://blog.csdn.net/zouxy09/article/details/8775360

  6. 浅谈java性能分析

    浅谈java性能分析,效能分析 在老师强烈的要求下做了效能分析,对上次写过的词频统计的程序进行分析以及改进. 对于效能分析:我个人很浅显的认为就是程序的运行效率,代码的执行效率等等. java做性能测 ...

  7. IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )

    *****联系人的界面的优化 HMContactsTableViewController.m #import "HMContactsTableViewController.h" # ...

  8. mac El Capitan 10.11.6

    http://bbs.pcbeta.com/forum.php?mod=viewthread&tid=1702502&authorid=4532202&page=1

  9. JS判断终端设备跳转PC端、移动端相应的URL

    <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...

  10. Android Glide数据更新及内存缓存、硬盘缓存清理

    [转] 原文                                         Android Glide数据更新及内存缓存.硬盘缓存清理 Android的Glide在加载图片时候内部默 ...