温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-04-24 10:44:47 来源:亿速云 阅读:153 作者:iii 栏目:开发技术

本篇内容介绍了“C#怎么实现XML文件与DataTable、Dataset互转”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、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#怎么实现XML文件与DataTable、Dataset互转”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI