91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

封开县| 苏尼特左旗| 交城县| 中牟县| 荥经县| 青神县| 洛宁县| 本溪| 邯郸市| 台中市| 濮阳县| 新巴尔虎左旗| 翼城县| 麻栗坡县| 泉州市| 额敏县| 丹阳市| 威远县| 马公市| 齐齐哈尔市| 旺苍县| 昌宁县| 革吉县| 南投市| 高碑店市| 阿拉善左旗| 铅山县| 鄄城县| 汨罗市| 余姚市| 兰西县| 江华| 施秉县| 荆门市| 玉环县| 晋城| 宜川县| 当阳市| 铁岭市| 甘谷县| 铅山县|