C # parsing XML to DataSet and dataset to XML file functions

Source: Internet
Author: User

Tag:io   ar   os   sp    file    on    div   bs   ad   

Converts an XML object content string to a dataset        public static DataSet Convertxmltodataset ( String xmlData)         {             stringreader stream = null;             xmltextreader reader = null;             try            {                 dataset XmlDS = new DataSet ();                 stream = new StringReader ( XmlData);                 // Load from stream to xmltextreader                 reAder = new XmlTextReader (stream);                 xmlds.readxml (reader);                 return xmlds;             }            catch (System.Exception ex)              {                 throw ex;             }            finally             {        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (reader! = null)          &nBsp;           reader. Close ();            }         }         //converting an XML file to 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);                 // Load from stream to xmltextreader                 reader = new XmlTextReader (stream);                 xmlds.readxml (reader);                 //xmlds.readxml (xmlfile);                 return xmlds;             }    &nBsp;       catch (System.Exception ex)              {                 throw ex;            }             finally             {            &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (reader! = null)                      reader. Close ();            }         }         //Converting a DataSet to an XML object string          public static string CONVERTDAtasettoxml (DataSet xmlds)         {             memorystream stream = null;             xmltextwriter writer = null;             try            {                 stream = new MemoryStream ();                 // Load from stream to xmltextreader                 writer = new XmlTextWriter (stream, Encoding.unicode);                  //writing to the file using the WriteXml method .           &nbsP;     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 ();            }         }         //Converting a DataSet to an XML file          public static void Convertdatasettoxmlfile (DataSet xmlds, string xmlfile)      &nBsp;   {            memorystream stream = Null;            xmltextwriter writer = null;             try             {                 stream = new MemoryStream ();                 //loading from stream to xmltextreader                 writer = new XmlTextWriter (stream, Encoding.unicode);                  // Write the file using the WriteXml method .                 xmlds.writexml (writeR);                 int count = ( int) stream. Length;                byte[] arr = new byte[count];                 stream. Seek (0, Seekorigin.begin);                 stream. Read (arr, 0, Count);                  //return Unicode-encoded text                  unicodeencoding UTF = new UnicodeEncoding ();                 streamwriter SW = new StreamWriter (xmlfile);                  SW. WriteLine ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>");         &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. WriteLine (UTF. GetString (arr). Trim ());                 sw. Close ();            }             catch (System.Exception ex)              {                 throw ex;            }             finally             {                 if (writer! = null)                      writer. Close ();            }         }

C # Parsing XML to DataSet and dataset to XML file functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.