Conversion between a C # datatable and an XML file

Source: Internet
Author: User

/// <summary>///DataTable goto XML File/// </summary>/// <param name= "dttable" ></param>/// <param name= "Strxmlpath" ></param>/// <returns></returns> Public BOOLDatatabletoxml (DataTable dttable,stringStrxmlpath) {MemoryStream stream=NULL; XmlTextWriter writer=NULL;D ataset DS=NewDataSet ();d S. Tables.add (Dttable.copy ()); StreamWriter SW=NULL;Try{Stream=NewMemoryStream (); writer=NewXmlTextWriter (Stream, Encoding.UTF8);d S. WRITEXML (writer, xmlwritemode.writeschema);intNcount = (int) stream. Length;byte[] arr =New byte[Ncount];stream. Seek (0, seekorigin.begin); stream. Read (arr,0, ncount); UTF8Encoding UTF=Newutf8encoding ();stringStrcontent =UTF. GetString (arr). Trim (); SW=NewStreamWriter (strxmlpath); SW. Write (strcontent);return true;}Catch(System.Exception verr) {MessageBox.Show (verr.message);return false;}finally{if(Writer! =NULL) {writer. Close ();}if(SW! =NULL) {SW. Close (); }}}/// <summary>///read XML to DataTable/// </summary>/// <param name= "Strxmlpath" ></param>/// <returns></returns> PublicDataTable xmltodatatable (stringStrxmlpath) {StringReader stream=NULL; XmlTextReader reader=NULL; StreamReader SR=NULL;Try{if(Strxmlpath.length <=0){return NewDataTable ();} SR=NewStreamReader (strxmlpath);stringStrxmlcontent =Sr. ReadToEnd (); Stream=NewStringReader (strxmlcontent); Reader=NewXmlTextReader (Stream);D ataset ds=NewDataSet ();d S. READXML (reader);returnDs. tables[0];}Catch(System.Exception verr) {MessageBox.Show (verr.message);}finally{if(SR! =NULL) Sr. Close ();if(Reader! =NULL) reader. Close ();}return NewDataTable ();}

Conversion between a C # datatable and an XML file

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.