Asp.net uses DataSet ReadXml to read XML files and Stream streams. datasetreadxml

Source: Internet
Author: User

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

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.