亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#怎么實現XML文件與DataTable、Dataset互轉

發布時間:2022-04-24 10:44:47 來源:億速云 閱讀:183 作者: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

阜宁县| 溆浦县| 微山县| 黄陵县| 平塘县| 古丈县| 霍州市| 广宗县| 凌源市| 邢台县| 五莲县| 吐鲁番市| 宣汉县| 获嘉县| 隆尧县| 息烽县| 涿鹿县| 淮北市| 平定县| 哈尔滨市| 大新县| 连州市| 水城县| 金阳县| 千阳县| 波密县| 凤冈县| 溆浦县| 五原县| 克拉玛依市| 平顺县| 涿州市| 仁寿县| 龙南县| 辽宁省| 兰坪| 惠州市| 东乡| 崇文区| 田阳县| 通海县|