c#解析XML到DATASET及dataset轉為xml檔案函數

來源:互聯網
上載者:User

標籤:io   ar   os   sp   檔案   on   div   bs   ad   

//將xml對象內容字串轉換為DataSet        public static DataSet ConvertXMLToDataSet(string xmlData)        {            StringReader stream = null;            XmlTextReader reader = null;            try            {                DataSet xmlDS = new DataSet();                stream = new StringReader(xmlData);                //從stream裝載到XmlTextReader                reader = new XmlTextReader(stream);                xmlDS.ReadXml(reader);                return xmlDS;            }            catch (System.Exception ex)            {                throw ex;            }            finally            {                if (reader != null)                    reader.Close();            }        }         //將xml檔案轉換為DataSet        public static DataSet ConvertXMLFileToDataSet(string xmlFile)        {            StringReader stream = null;            XmlTextReader reader = null;            try            {                XmlDocument xmld = new XmlDocument();                xmld.Load(xmlFile);                 DataSet xmlDS = new DataSet();                stream = new StringReader(xmld.InnerXml);                //從stream裝載到XmlTextReader                reader = new XmlTextReader(stream);                xmlDS.ReadXml(reader);                //xmlDS.ReadXml(xmlFile);                return xmlDS;            }            catch (System.Exception ex)            {                throw ex;            }            finally            {                if (reader != null)                    reader.Close();            }        }         //將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到DATASET及dataset轉為xml檔案函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.