I have also learned some of this knowledge before. I haven't used it for a long time. I forgot about it. Now I can consolidate it again and familiarize myself with the operations on XML files.
XML (Extensible Markup Language) can be used to expand the markup language. Like HTML, It is SGML (Standard Generalized Markup Language, standard common Markup Language ). XML is cross-platform in the Internet environment and relies on content technology. It is a powerful tool for processing structured document information.
XML is a simple data storage language that uses a series of simple tags to describe data. These tags can be conveniently created, although XML occupies more space than binary data, XML is extremely easy to master and use.
XML data types include: element, attribute, comment, text
For example, the next simple XML file:
Code
Copy code The Code is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<Library>
<Books id = "computer">
<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 = "human">
<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 <book> </book>
Attribute attribute id = "computer"
Text content <Name> Title 2 </Name>
Generally, we need to search for the content contained in the corresponding node based on the attribute value in the XML file. To avoid the loops of many nested nodes, we can use the following method to find the desired node.
Xmldocument xdoc = new xmldocument (); // create a Document Object
Xdoc. Load ("book. xml"); // load the XML file
String condition = "// books [@ ID = 'computer class'] // book [@ ID = '000000']"; // conditions for finding nodes
Xmlnode node = xdoc. documentelement. selectsinglenode (condition); // return a node that meets the conditions
Foreach (xmlnode xnode1 in node)
{
// All subnodes under this node
Xmlnodelist xnodelist2 = node. childnodes;
}
Query condition description:
1. Use text () to obtain the text node
String condition = "// books [@ ID = 'computer class'] // book [@ ID = '000000'] // name // text ()";
2. Use the [] symbol to query nodes with specific conditions
String condition = "// books [@ ID = 'computer class'] // book [@ ID = '000000']";
3. Use the | symbol to get nodes in multiple modes
String condition = "// books [@ ID = 'computer class'] // book [@ ID = '000000'] | // books [@ ID = 'computer class'] // book [@ ID = '000000'] ";
4. Use the * symbol to return all the subnodes of the current node.
String condition = "// books [@ ID = 'computer class'] // * // name ";
Edit XML data:
1. Add an attribute node of an element.
Xmlnode xnodeatt = new xmlnode ();
Xnodeatt = xdoc. createattribute ("ID", book );
Xnodeatt. innerxml = "10003 ";
Objnode. Attributes. append (xnodeatt );
2. Delete the attribute of an element.
Objnode. Attributes. Remove (xatt );
3. Add a child element)
Xmlnode xnodeatt = new xmlnode ();
Xnodeatt = xdoc. createelement ("book", "book ");
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/