Talking about how ASP.net processes XML data and asp. netxml

Source: Internet
Author: User

Talking about how ASP.net processes XML data and asp. netxml

XML is an extensible markup language, which is more flexible than html mentioned earlier. Although it is only one letter different from HTML, there is a big difference between the two.

 

XML is also a markup language, so each tag must be closed, while HTML does not have much impact if it is forgotten occasionally (it is not recommended that you omit it here. Good writing standards are still required)

Secondly, XML can be called plain text. Its main function is not to be directly displayed on a webpage, but to be used as a data storage or data transmission tool. When it comes to the data layer, XML is very important. Some data does not need SQL, while El tries not to use large databases. XML is necessary.

 

This is an example written by myself. It involves ASP. NET reading and displaying XML. You can take a look.

My example link: http://download.csdn.net/detail/u010792238/7001785

 

There is nothing to mention about XML syntax and norms. The following is an example of ASP.net's XML Manipulation:

Special attention? There must be no space between and xml; otherwise, an error will occur during running!

<? Xml version = "1.0" encoding = "GB2312"?> // Save As course. xml

<Courses>

<Course>

<Id> 1 </id>

<Title> Tom Cruise </title>

<Url> mission impossible.mp3 </url>

</Course>

<Course>

<Id> 2 </id>

<Title> Leonardo DiCaprio </title>

<Url> my heart will go onboarding </url>

</Course>

</Courses>

DataSet provides methods to process XML documents: ReadXML reading, WriteXML writing, and so on.

Objective: To read the content in the XML document to DataSet and display it in the GridView. (Note that the System. Data and System. Data. OleDb namespaces are imported)

 

Protected void Page_Load (object sender, EventArgs e)

{

DataSet DS = new DataSet (); // create a DataSet object

DS. ReadXML (Server. MapPath ("course. xml"); // read the XML document

GridView1.DataSource = DS. Tables [0]. DefaultView;

GridView1.DataBind ();

}


How does aspnet read and write data to xml?

This article describes how to read XML files in ASP. NET applications. With this technique, we can customize the configuration file of our application and read the data stored in the XML file.

Overview

The following code uses the XmlTextReader object to read data from disk files to the XmlDocument object. The XmlTextReader object is very similar in function to StreamReader and BinaryReader objects, but it is specially designed to read XML files. In addition, the XmlTextReader object has other XMl-related features. For example, the WhitespaceHandling attribute used in the Code tells the application not to create nodes for unnecessary spaces in the XML file.

The following code uses the DocumentElement attribute of the XmlTextReader object to find the root node in the tree expression of the XML document. Call the AddWithChildren method recursively to add the node and its sub-nodes to The listbox.

The following code also contains the processing of attributes. Attribute nodes are not included in the child node set of an XmlDocument object node. Therefore, you can only use the Attributes attribute of the XmlNode object to obtain the attribute node set. After obtaining the attribute node set, the Code uses the XmlNamedNodeMap object to save the set. This object can save any set of XmlNode objects of any type.

Code list

Private void btnLoad_Click (object sender, System. EventArgs e)

{

XmlTextReader reader = new XmlTextReader (

Server. MapPath ("mycompany. xml "));

Reader. WhitespaceHandling = WhitespaceHandling. None;

XmlDocument xmlDoc = new XmlDocument ();

// Load the file into the XmlDocument object

XmlDoc. Load (reader );

// Close the connection

Reader. Close ();

// Add elements representing documents to listbox

LbNodes. Items. Add ("XML Document ");

// Search for the root node and add it and its sub-nodes to listbox

XmlNode xnod = xmlDoc. DocumentElement;

AddWithChildren (xnod, 1 );

}

Private void AddWithChildren (XmlNode xnod, Int32 intLevel)

{

// Add the node and Its subnodes to listbox together

// IntLevel controls the indent depth

XmlNode xnodWorking;

String strIndent = new string (''', 2 * intLevel );

// Read the value of a node if it has a value

String strValue = (string) xnod. Value;

If (strValue! = Null)

{

StrValue = ":" + strValue;

}

// Add node details to ListBox

LbNodes. Items ...... remaining full text>

In aspnet, xml is used as the data source and frequent changes are made.

If this XML file is not large, it will theoretically be more efficient than using relational databases, and frequent operations will also cause great problems. However, when the number of XML files reaches a certain level, the first cause management problems. The second cause is that disk read/write may become a bottleneck if common file systems are used for storage. In addition, you must control the exclusive access to XML files in the program.

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.