asp.net XML file Operation implementation code _ Practical skills

Source: Internet
Author: User
Tags net xml
has also learned some of this knowledge, long time no how to use, forget about the same, just now can be consolidated again, familiar with the operation of the XML file.
XML (extensible Markup Language) expands the markup language, which, like HTML, is SGML (Standard generalized Markup Language, Standard Universal Markup Language). XML is a cross-platform, content-dependent technology in an Internet environment and a powerful tool for dealing with structured document information at the moment.
Extensible Markup Language XML is a simple data storage language that uses a series of simple tags to describe data that can be built in a convenient way, although XML takes up more space than binary data, but XML is extremely simple and easy to master and use.
XML data types are: Element,attribute,comment,text
The following is a simple XML file:
Code
Copy Code code as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<library>
<books id= "Computer class" >
<book id= "10001" >
<name> title 1</name>
<publish> Press 1</publish>
<price> Price 1</price>
</book>
<book id= "10002" >
<name> title 2</name>
<publish> Press 2</publish>
<price> Price 2</price>
</book>
</books>
<books id= "Humanities" >
<book id= "20001" >
<name> title 1</name>
<publish> Press 1</publish>
<price> Price 1</price>
</book>
<book id= "20002" >
<name> title 2</name>
<publish> Press 2</publish>
<price> Price 2</price>
</book>
</books>
</library>

Element elements <book></book>
Attribute Property id= "Computer class"
Text content <name> Title 2</name>

In general, we want to go to the XML file according to the attribute value to find the content of the corresponding node, in order to avoid nesting many nodes of the loop, we can use the following method to find the desired node.
XmlDocument xdoc = new XmlDocument (); To create a Document object
Xdoc. Load ("Book.xml"); Load XML file
String Condition = "//books[@id = ' Computer class ']//book[@id = ' 10001 ']"; Conditions to find nodes
XmlNode node = Xdoc. Documentelement.selectsinglenode (Condition); Returns the node that meets the criteria
foreach (XmlNode xnode1 in node)
{
All child nodes under this node
XmlNodeList xNodeList2 = node. ChildNodes;
}

Query Condition Description:
1. Use text () to get the text node
String Condition = "//books[@id = ' Computer class ']//book[@id = ' 10001 ']//name//text ()";
2. Use the [] symbol to query the node for a specific condition
String Condition = "//books[@id = ' Computer class ']//book[@id = ' 10001 ']";
3. Use | A symbol can get multiple patterns of nodes
String Condition = "//books[@id = ' Computer class ']//book[@id = ' 10001 '] | books[@id = ' Computer class ']//book[@id = ' 10002 '];
4. Use the * symbol to return all child nodes of the current node
String Condition = "//books[@id = ' Computer class ']//*//name ';
 
Editing of XML data:
1. Add an attribute node to an element
XmlNode Xnodeatt = new XmlNode ();
Xnodeatt = Xdoc.createattribute ("id", book);
Xnodeatt.innerxml = "10003";
ObjNode.Attributes.Append (Xnodeatt);
2. Delete an element's properties
ObjNode.Attributes.Remove (Xatt);
3. Add a child element
XmlNode Xnodeatt = new XmlNode ();
Xnodeatt = Xdoc.createelement ("book", "Books");
Xnodeatt.innerxml = "10003";
ObjNode.Attributes.Append (Xnodeatt);
4. Delete a child element
Objnode.removechild (Nodechild);
5. Replace a child element
Objnode.replacechild (Newchild,oldchile);
From: http://zhf.cnblogs.com/
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.