Asp.net uses DataSet ReadXml to read XML files and Stream streams. datasetreadxml
This example describes how asp.net uses DataSet's ReadXml to read XML files and Stream streams. We will share this with you for your reference. The details are 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(Encoding.Default.GetBytes(strxml));ds.ReadXml(stream);GridView1.DataSource = ds;GridView1.DataBind();
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> <artist> Babyface </artist> <genre> R & B </genre> <album> unknown </album> <year> </song> <artist> Babyface </artist> <genre> R & B </genre> <album> essential babyface </album> <year> 2001 </year> </song> <artist> Babyface </artist> <genre> R & B </genre> <album> Grown and sexy </album> <year> 2005 </year> </song> <artist> Maria Arredondo </artist> <genre> Pop </genre> <album> Not going under </album> <year> 2004 </year> </song> <artist> Leona Lewis </artist> <genre> Pop </genre> <album> Unknown </album> <year> 2008 </year> </song> <artist> Usher </artist> <genre> R & B </genre> <album> Usher </album> <year> 2008 </year> </song> <artist> Christina Aguilera </artist> <genre> Blues </genre> <album> Back to basics </album> <year> 2004 </year> </song> <artist> Sting </artist> <genre> pop </genre> <album> Shape of my heart </album> <year> </song> </music> "); // read the Xml string to receive the data returned by WebService ds. readXml (reader, XmlReadMode. auto); // generate an Xml file // ds. writeXml (Server. mapPath ("xml/song_bak.xml"); GridView1.DataSource = ds; GridView1.DataBind ();}}
# Convert the Xml returned by the region interface to DataSet /// <summary> /// convert the returned Xml to DataSet /// </summary> /// <param name = "text"> xml character </param> /// <returns> </returns> private DataSet GetDataSet (string text) {try {XmlTextReader reader = new XmlTextReader (new StringReader (text); reader. whitespaceHandling = WhitespaceHandling. none; DataSet ds = new DataSet (); ds. readXml (reader); reader. close (); ds. dispose (); return ds ;}catch {return null ;}} # endregion # The region background submits data and obtains the data returned by the interface. // <summary> // The background submits the 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 = (System. net. httpWebRequest) WebRequest. create (requestUrl); request. method = "GET"; StreamReader jsonStream = new StreamReader (request. getResponse (). getResponseStream (); string jsonObject = jsonStream. readToEnd (); return jsonObject;} catch {return string. empty ;}# endregion