SoFunction
Updated on 2025-03-10

How to use DataSet's ReadXml to read XML files and Stream streams

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(@"
          &lt;music&gt;
           &lt;song&gt;
            &lt;artist&gt;The Chi-lites&lt;/artist&gt;
            &lt;genre&gt;Soul&lt;/genre&gt;
            &lt;album&gt;A lonely man&lt;/album&gt;
            &lt;year&gt;1972&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Babyface&lt;/artist&gt;
            &lt;genre&gt;R&amp;B&lt;/genre&gt;
            &lt;album&gt;unknown&lt;/album&gt;
            &lt;year&gt;&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Babyface&lt;/artist&gt;
            &lt;genre&gt;R&amp;B&lt;/genre&gt;
            &lt;album&gt;The essential babyface&lt;/album&gt;
            &lt;year&gt;2001&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Babyface&lt;/artist&gt;
            &lt;genre&gt;R&amp;B&lt;/genre&gt;
            &lt;album&gt;Grown and sexy&lt;/album&gt;
            &lt;year&gt;2005&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Maria Arredondo&lt;/artist&gt;
            &lt;genre&gt;Pop&lt;/genre&gt;
            &lt;album&gt;Not going under&lt;/album&gt;
            &lt;year&gt;2004&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Leona Lewis&lt;/artist&gt;
            &lt;genre&gt;Pop&lt;/genre&gt;
            &lt;album&gt;Unknown&lt;/album&gt;
            &lt;year&gt;2008&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Usher&lt;/artist&gt;
            &lt;genre&gt;R&amp;B&lt;/genre&gt;
            &lt;album&gt;Usher&lt;/album&gt;
            &lt;year&gt;2008&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Christina Aguilera&lt;/artist&gt;
            &lt;genre&gt;Blues&lt;/genre&gt;
            &lt;album&gt;Back to basics&lt;/album&gt;
            &lt;year&gt;2004&lt;/year&gt;
           &lt;/song&gt;
           &lt;song&gt;
            &lt;artist&gt;Sting&lt;/artist&gt;
            &lt;genre&gt;Pop&lt;/genre&gt;
            &lt;album&gt;Shape of my heart&lt;/album&gt;
            &lt;year&gt;&lt;/year&gt;
           &lt;/song&gt;
          &lt;/music&gt;
          ");
    //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/// &lt;summary&gt;
/// The returned Xml is converted to DataSet/// &lt;/summary&gt;
/// <param name="text">Xml characters</param>/// &lt;returns&gt;&lt;/returns&gt;
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/// &lt;summary&gt;
/// The background submits data and obtains the data returned by the interface/// &lt;/summary&gt;
/// <param name="relativePath">Address</param>/// &lt;returns&gt;&lt;/returns&gt;
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.