欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#实现XML文件与DataTable、Dataset互转

时间:2022-06-26 10:34:18|栏目:.NET代码|点击:

一、DataTable转XML

        #region DataTableToXml
        /// <summary>
        /// 将DataTable对象转换成XML字符串
        /// </summary>
        /// <param name="ds">DataSet对象</param>
        /// <returns>XML字符串</returns>
        public static string DataTableToXml(DataTable dt,string sName)
        {
            if (dt != null)
            {
                MemoryStream ms = null;
                XmlTextWriter XmlWt = null;
                try
                {
                    ms = new MemoryStream();
                    //根据ms实例化XmlWt
                    XmlWt = new XmlTextWriter(ms, System.Text.Encoding.Unicode);
                    //获取ds中的数据
                    dt.TableName = Sql.IsEmptyString(sName) ? "dt2xml" : sName;
                    dt.WriteXml(XmlWt, XmlWriteMode.WriteSchema);
                    int count = (int)ms.Length;
                    byte[] temp = new byte[count];
                    ms.Seek(0, SeekOrigin.Begin);
                    ms.Read(temp, 0, count);
                    //返回Unicode编码的文本
                    System.Text.UnicodeEncoding ucode = new System.Text.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 "";
            }
        }
        #endregion

二、XML转Dataset

方法A:

        #region Xml To DataSet
        public static DataSet XmlToDataSet(string xmlString)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlString);
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmldoc.InnerXml);
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                reader.Close();
                return xmlDS;
            }
            catch (System.Exception ex)
            {
                reader.Close();
                throw ex;
            }
        }
        #endregion

方法B:

        private static  DataSet XMLToDataset()
        {
            string strDBXMLFile = @"F:\TestDir\XML\DBTEST.XML";
            DataSet dsXML = new DataSet();
            dsXML.ReadXml(strDBXMLFile);
            //某个节点名称的所有节点内容
             DataTable dtOneNote = dsXML.Tables["SMT"];
            return dsXML;
        }

三、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();
                }
            }

上一篇:C#多线程TPL常见操作误区与异常处理

栏    目:.NET代码

下一篇:ASP.NET MVC项目部署方式介绍

本文标题:C#实现XML文件与DataTable、Dataset互转

本文地址:http://www.codeinn.net/misctech/205985.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有