How does asp.net read data from xml files? 2008-01-20 16: 42news. xml file: View Code
1 <? Xml version = "1.0" encoding = "gb2312"?> <Xinwen> <news> <news_id> 5 </news_id> <news_title> XML news Publishing System </news_title> <news_author> *** </news_author> <news_ly> original </news_ly> <news_content> the XML news publishing system has been tested successfully! </News_content> <news_adddate> 15:39:24 </news_adddate> </news> </xinwen>
How can we read data from xml files? View Code
1 private void ReadXML ()
2 {
3 string url = Server. MapPath ("news. xml"); // obtain the XML file in the current folder
4 StreamReader sRead = new StreamReader (url, System. Text. Encoding. GetEncoding ("GB2312 "));
5 // read characters from the byte stream with a specific encoding. The characters must be converted to GB2312 for reading.
6 XmlDataDocument datadoc = new XmlDataDocument (); // operate XML documents
7 datadoc. DataSet. ReadXml (sRead); // Save the read byte stream to DataSet.
8 this. GridView1.DataSource = datadoc. DataSet. Tables [0]. DefaultView;
9 this. GridView1.DataBind ();
10 datadoc = null; // clear the XML data operation
11 sRead. Close (); // Close byte stream reading
12}
The principle is to use dataset to load the data in the xml file and use it as the data source of the gridview control.