This article describes the method of using DataSet's ReadXml to read XML files and Stream streams. Share it for your reference, as follows:
string strxml = "<xml><m><a>1</a><b>2</b></m><m><a>11</a><b>22</b></m><m><a>111</a><b>222</b></m></xml>"; DataSet ds = new DataSet(); Stream stream = new MemoryStream((strxml)); (stream); = ds; ();
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataSet ds = new DataSet(); TextReader reader = new StringReader(@" <music> <song> <artist>The Chi-lites</artist> <genre>Soul</genre> <album>A lonely man</album> <year>1972</year> </song> <song> <artist>Babyface</artist> <genre>R&B</genre> <album>unknown</album> <year></year> </song> <song> <artist>Babyface</artist> <genre>R&B</genre> <album>The essential babyface</album> <year>2001</year> </song> <song> <artist>Babyface</artist> <genre>R&B</genre> <album>Grown and sexy</album> <year>2005</year> </song> <song> <artist>Maria Arredondo</artist> <genre>Pop</genre> <album>Not going under</album> <year>2004</year> </song> <song> <artist>Leona Lewis</artist> <genre>Pop</genre> <album>Unknown</album> <year>2008</year> </song> <song> <artist>Usher</artist> <genre>R&B</genre> <album>Usher</album> <year>2008</year> </song> <song> <artist>Christina Aguilera</artist> <genre>Blues</genre> <album>Back to basics</album> <year>2004</year> </song> <song> <artist>Sting</artist> <genre>Pop</genre> <album>Shape of my heart</album> <year></year> </song> </music> "); //Read the Xml string to receive the data returned by WebService (reader, ); //Generate Xml file //(("xml/song_bak.xml")); = ds; (); } }
The Xml returned by #region interface is converted into DataSet/// <summary> /// The returned Xml is converted to DataSet/// </summary> /// <param name="text">Xml characters</param>/// <returns></returns> private DataSet GetDataSet(string text) { try { XmlTextReader reader = new XmlTextReader(new StringReader(text)); = ; DataSet ds = new DataSet(); (reader); (); (); return ds; } catch { return null; } } #endregion #region The background submits data and obtains the data returned by the interface/// <summary> /// The background submits data and obtains the data returned by the interface/// </summary> /// <param name="relativePath">Address</param>/// <returns></returns> public static string GetRequestString(string relativePath) { string requestUrl = relativePath; try { // Create an HTTP request HttpWebRequest request = ()(requestUrl); = "GET"; StreamReader jsonStream = new StreamReader(().GetResponseStream()); string jsonObject = (); return jsonObject; } catch { return ; } } #endregion
For more information about relevant content, please check out the topic of this site:Summary of operation json skills》、《Summary of string operation techniques》、《Summary of operating XML skills》、《Summary of file operation skills》、《Ajax tips summary topic"and"Summary of cache operation skills》。
I hope this article will be helpful to everyone's programming.