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