asp.net methods for reading XML files and stream streams using the ReadXml of a dataset _ practical tips

Source: Internet
Author: User

The example in this article describes the ASP.net method of reading XML files and stream streams using the dataset's ReadXml. Share to everyone for your reference, specific 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> <song> &LT;ARTIST&GT;BABYFACE&L t;/artist> <genre>R&B</genre> <album>unknown</album> &L 
            t;year></year> </song> <song> <artist>Babyface</artist>
            <genre>R&B</genre> <album>the Essential Babyface</album> <year>2001</year> </song> <song> <artist>babyface</arti St> <genre>
           R&b</genre> <album>grown and Sexy</album> <year>2005</year> </song> <song> <artist>maria arredondo</artist> <genr
           E>pop</genre> <album>not going under</album> <year>2004</year> </song> <song> <artist>leona lewis</artist> <genre& Gt Pop</genre> <album>Unknown</album> <year>2008</year> < /song> <song> <artist>Usher</artist> <genre>r&b</genr
           e> <album>Usher</album> <year>2008</year> </song> <song> <artist>christina aguilera</artist> &LT;GENRE&GT;BLUES&LT;/GENRE&G
            T <aLbum>back to basics</album> <year>2004</year> </song> <son g> <artist>Sting</artist> <genre>Pop</genre> <album>s
          Hape of my heart</album> <year></year> </song> </music>
    "); Read the XML string used to receive the WebService return data ds.
    READXML (reader, Xmlreadmode.auto); Generates an XML file//DS.
    WriteXml (Server.MapPath ("Xml/song_bak.xml"));
    Gridview1.datasource = ds;
  Gridview1.databind ();

 }
}
XML converted to DataSet///<summary>///returned by #region interface to DataSet///</summary>///<param name= "text" >xm L character </param>///<returns></returns> private DataSet GetDataSet (string text) {try {Xmltextreade
    R 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 #region the background to submit the data and get the data returned by the interface///<summary>///the backend data and get 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 requests = (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

More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."

I hope this article will help you to ASP.net program design.

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.