How to realize the conversion between XML File, DataTable and Dataset in C #
This article introduces the relevant knowledge of "how to realize the conversion between XML files and DataTable and Dataset in C #". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. DataTable to XML # region DataTableToXml / convert DataTable object to XML string / DataSet object / XML string public static string DataTableToXml (DataTable dt,string sName) {if (dt! = null) {MemoryStream ms = null XmlTextWriter XmlWt = null; try {ms = new MemoryStream (); / / instantiate XmlWt XmlWt = new XmlTextWriter (ms, System.Text.Encoding.Unicode) according to ms / / get the data in ds dt.TableName = Sql.IsEmptyString (sName)? "dt2xml": sName; dt.WriteXml (XmlWt, XmlWriteMode.WriteSchema); int count = (int) ms.Length; byte [] temp = new byte [count]; ms.Seek (0, SeekOrigin.Begin); ms.Read (temp, 0, count) / / return Unicode encoded text System.Text.UnicodeEncoding ucode = new System.Text.UnicodeEncoding (); string returnValue = ucode.GetString (temp) .Trim (); return returnValue;} catch (System.Exception ex) {throw ex } finally {/ / release resource if (XmlWt! = null) {XmlWt.Close (); ms.Close (); ms.Dispose () } else {return ";}} # endregionII, XML to Dataset
Method A:
# region XmlToDataSet public static DataSet XmlToDataSet (string xmlString) {XmlDocument xmldoc = new XmlDocument (); xmldoc.LoadXml (xmlString); StringReader stream = null; XmlTextReader reader = null; try {DataSet xmlDS = new DataSet (); stream = new StringReader (xmldoc.InnerXml) Reader = new XmlTextReader (stream); xmlDS.ReadXml (reader); reader.Close (); return xmlDS;} catch (System.Exception ex) {reader.Close (); throw ex;}} # endregion
Method B:
Private static DataSet XMLToDataset () {string strDBXMLFile = @ "F:\ TestDir\ XML\ DBTEST.XML"; DataSet dsXML = new DataSet (); dsXML.ReadXml (strDBXMLFile); / / all node contents of a node name DataTable dtOneNote = dsXML.Tables ["SMT"]; return dsXML } third, Dataset to XML 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) / / write the file with 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); UnicodeEncoding utf = new UnicodeEncoding (); return utf.GetString (arr). Trim () } catch (System.Exception ex) {throw ex;} finally {if (writer! = null) writer.Close () 4. Dataset to XML file public static void ConvertDataSetToXMLFile (DataSet xmlDS, string xmlFile) {MemoryStream stream = null; XmlTextWriter writer = null; try {stream = new MemoryStream () / / load 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 (""); sw.WriteLine (utf.GetString (arr). Trim ()); sw.Close ();} catch (System.Exception ex) {throw ex } finally {if (writer! = null) writer.Close ();}} "how to convert XML files to DataTable and Dataset" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!