C # Reading xml and DataTable conversion,

Source: Internet
Author: User

C # Reading xml and DataTable conversion,

1. Methods for reading xml files

/// <Summary> /// convert xml to Datable /// </summary> public static DataTable XmlToDataTable (string xmlStr) {if (! String. isNullOrEmpty (xmlStr) {StringReader StrStream = null; XmlTextReader Xmlrdr = null; try {DataSet ds = new DataSet (); // read the information in the string StrStream = new StringReader (xmlStr ); // obtain the data in StrStream Xmlrdr = new XmlTextReader (StrStream); // ds obtains the data ds in Xmlrdr. readXml (Xmlrdr); return ds. tables [0];} catch (Exception e) {return null;} finally {// release the resource if (Xmlrdr! = Null) {Xmlrdr. Close (); StrStream. Close (); StrStream. Dispose () ;}} return null ;}
Read xml

2. Convert dataTable to xml and save it to the root directory.

/// <Summary> /// convert datatable to xml // </summary> public static void DataTableToXml (DataTable vTable) {string savePath = Application. startupPath. toString (); if (! Directory. exists (savePath) {Directory. createDirectory (savePath);} string xml = savePath + @ "\ group info table. xml "; // if the file DataTable. if xml exists, delete if (File. exists (xml) {File. delete (xml);} vTable. writeXml (savePath + @ "\ group info table. xml ");}
DataTable write xml file

 

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.