It is known that there is an XML file (bookstore. xml) as follows:
Copy codeThe Code is as follows: <? Xmlversion = "1.0" encoding = "gb2312"?>
<Bookstore>
<Bookgenre = "fantasy" ISBN = "2-3631-4">
<Title> Oberon 'slegacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
</Bookstore>
1. Insert a <book> node to the <bookstore> node:Copy codeThe Code is as follows: XmlDocumentxmlDoc = newXmlDocument ();
XmlDoc. Load ("bookstore. xml ");
XmlNoderoot = xmlDoc. SelectSingleNode ("bookstore"); // search <bookstore>
XmlElementxe1 = xmlDoc. createElement_x ("book"); // create a <book> node
Xe1.SetAttribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
Xe1.SetAttribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.
XmlElementxesub1 = xmlDoc. createElement_x ("title ");
Xesub1.InnerText = "CS from entry to entry"; // set a text node
Xe1.AppendChild (xesub1); // Add it to the <book> node
XmlElementxesub2 = xmlDoc. createElement_x ("author ");
Xesub2.InnerText = "Hou Jie ";
Xe1.AppendChild (xesub2 );
XmlElementxesub3 = xmlDoc. createElement_x ("price ");
Xesub3.InnerText = "58.3 ";
Xe1.AppendChild (xesub3 );
Root. AppendChild (xe1); // Add it to the <bookstore> node
XmlDoc. Save ("bookstore. xml ");
// ==========================
Result:
Copy codeThe Code is as follows:
<? Xmlversion = "1.0" encoding = "gb2312"?>
<Bookstore>
<Bookgenre = "fantasy" ISBN = "2-3631-4">
<Title> Oberon 'slegacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Bookgenre = "Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Book>
</Bookstore>
2. Modify the node: Change the genre value of the node whose genre attribute value is "Li zenhong" to "update Li zenhong ", modify the text of the child node <author> of the node to "Yasheng ".
Copy codeThe Code is as follows: XmlNodeListnodeList = xmlDoc. SelectSingleNode ("bookstore"). ChildNodes; // obtain all the subnodes of the bookstore Node
Foreach (XmlNodexninnodeList) // traverses all subnodes
{
XmlElementxe = (XmlElement) xn; // converts the subnode type to the XmlElement type.
If (xe. GetAttribute ("genre") = "") // if the genre attribute value is ""
{
Xe. SetAttribute ("genre", "update lizan red"); // you can change this attribute to "update lizan red"
XmlNodeListnls = xe. ChildNodes; // continue to obtain all the child nodes of the xe subnode
Foreach (XmlNodexn1innls) // traverse
{
XmlElementxe2 = (XmlElement) xn1; // Conversion Type
If (xe2.Name = "author") // if you find
{
Xe2.InnerText = "Yasheng"; // modify
Break; // find and exit.
}
}
Break;
}
}
XmlDoc. Save ("bookstore. xml"); // Save.
// ==============================
The final result is:
Copy codeThe Code is as follows: <? Xmlversion = "1.0" encoding = "gb2312"?>
<Bookstore>
<Bookgenre = "fantasy" ISBN = "2-3631-4">
<Title> Oberon 'slegacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Bookgenre = "update Li zanhong" ISBN = "2-3631-4">
<Title> CS from entry to entry </title>
<Author> Yasheng </author>
<Price> 58.3 </price>
</Book>
</Bookstore>
3. Delete the genre attribute of the <bookgenre = "fantasy" ISBN = "2-3631-4"> node, delete the <bookgenre = "update lizanhong" ISBN = "2-3631-4"> node.
Copy codeThe Code is as follows: XmlNodeListxnl = xmlDoc. SelectSingleNode ("bookstore"). ChildNodes;
Foreach (XmlNodexninxnl)
{
XmlElementxe = (XmlElement) xn;
If (xe. GetAttribute ("genre") = "fantasy ")
{
Xe. RemoveAttribute ("genre"); // Delete genre attributes
}
Elseif (xe. GetAttribute ("genre") = "update lizanhong ")
{
Xe. RemoveAll (); // delete all content of the node
}
}
XmlDoc. Save ("bookstore. xml ");
// ==================================
The final result is:
Copy codeThe Code is as follows: <? Xmlversion = "1.0" encoding = "gb2312"?>
<Bookstore>
<BookISBN = "2-3631-4">
<Title> Oberon 'slegacy </title>
<Author> Corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book>
</Book>
</Bookstore>
4. display all data.
Copy codeThe Code is as follows: XmlNodexn = xmlDoc. SelectSingleNode ("bookstore ");
XmlNodeListxnl = xn. ChildNodes;
Foreach (XmlNodexnfinxnl)
{
XmlElementxe = (XmlElement) xnf;
Console. WriteLine (xe. GetAttribute ("genre"); // display attribute values
Console. WriteLine (xe. GetAttribute ("ISBN "));
XmlNodeListxnf1 = xe. ChildNodes;
Foreach (XmlNodexn2inxnf1)
{
Console. WriteLine (xn2.InnerText); // displays the child node text.
}
}