asp.net XML file operation implementation code

  • 2020-05-07 19:27:51
  • OfStack

I also learned some knowledge of this aspect before, but I didn't use it for a long time, and I forgot it almost. Now I can reconsolidate it once more and get familiar with the operation of XML file under 1.
XML (Extensible Markup Language) extends the markup language, which, like HTML1, is SGML(Standard Generalized Markup Language, standard general-purpose markup language). Xml is a cross-platform, content-dependent technology in the Internet environment that is currently a powerful tool for processing structured document information.
XML is a simple data storage language that USES 1 series of simple tags to describe data. These tags can be created in a convenient way. Although XML takes up more space than binary data, XML is extremely simple and easy to learn and use.
The XML data types are: Element, Attribute, Comment, Text
The following is a simple XML file:
code
 
<?xml version="1.0" encoding="UTF-8"?> 
<library> 
<books id=" The computer class "> 
<book id="10001"> 
<name> Title: 1</name> 
<publish> Press. 1</publish> 
<price> The price 1</price> 
</book> 
<book id="10002"> 
<name> Title: 2</name> 
<publish> Press. 2</publish> 
<price> The price 2</price> 
</book> 
</books> 
<books id=" The humanities class "> 
<book id="20001"> 
<name> Title: 1</name> 
<publish> Press. 1</publish> 
<price> The price 1</price> 
</book> 
<book id="20002"> 
<name> Title: 2</name> 
<publish> Press. 2</publish> 
<price> The price 2</price> 
</book> 
</books> 
</library> 

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

Generally, we need to find the contents contained in the corresponding node according to the attribute value in the XML file. In order to avoid the loop of nesting many 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 xml file
string Condition = "//books[@id =' computer class ']//book[@id ='10001']"; // need to find the conditions of the node
XmlNode node = xdoc. DocumentElement. SelectSingleNode (Condition); // returns the qualified node
foreach (XmlNode xnode1 in node)
{
// all children under the node
XmlNodeList xNodeList2 = node.ChildNodes;
}

Description of query conditions:
1. Use text() to get the Text node
string Condition = "//books[@id =' computer class ']//book[@id =' computer class ']//name//text()";
2. Use the [] symbol to query a node for a specific condition
string Condition = "//books[@id =' computer class ']//book[@id ='10001']";
3. Multiple mode nodes can be obtained by using | symbols
string Condition = "//books[@id =' computer class ']//book[@id =' computer class '] | //books[@id =' computer class ']//book[@Language ='10002']";
4. Use the * symbol to return all children of the current node
string Condition = "//books[@id =' computer class ']//*//name";

Editing of XML data:
1. Add an attribute (Attribute) node of one element
XmlNode xNodeAtt = new XmlNode();
xNodeAtt = xDoc. CreateAttribute (" id book);
xNodeAtt. InnerXml = "10003";
objNode. Attributes. Append (xNodeAtt);
2. Delete the attributes of an element
objNode. Attributes. Remove (xAtt);
3. Add 1 child element (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);
Replace a child element
objNode. ReplaceChild (newChild oldChile);
From: http: / / zhf. cnblogs. com /

Related articles: