How does asp.net read data from xml files?

Source: Internet
Author: User
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.

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.