One XML file is known (bookstore.xml) is as follows:
XML Version = "1.0" encoding = "GB2312"?>
1. Insert a
XMLDocument Xmldoc = new xmldocument (); xmldoc.load ("BookStore.xml");
XMLNode root = xmldoc.selectsinglenode ("bookstore"); // Find
XMLELEMENT XESUB1 = XMLDoc.createElement ("Title"); Xesub1.innerText = "CS From Getting Started to Wen"; // Settings Text Node XE1.AppendChild (Xesub1); // Add to
Root.appendchild (xe1); // Add to
/ / =======================================================================================
The result is:
XML Version = "1.0" encoding = "GB2312"?>
XmlNodelist NodeList = XmLDoc.selectsinglenode ("BookStore"). ChildNodes; // Get all child nodes for the BookStore node Foreach (XMLNode Xn In nodelist) // Traverse all child nodes {xmlelement XE = (xmlelement) xn; // Fander node Type Convert to XMLELEMENT Type IF (Xe.GetaTRibute ("Genre") == "Li Zanhong") // If the GENRE attribute value is "Li Zanhong" {xe.setttribute ("genre", "Update Li Zan Hong"); // Modify This attribute is "Update Li Zan Hong"
XMLNodelist NLS = XE.ChildNodes; // Continue all child nodes of the XE child node Foreach (XMLNode XN1 in NLS) // Traversed {XMLELEMENT XE2 = (XMLELEMENT) XN1; // Conversion Type IF (Xe2.Name == "AUTHOR ") // If you find {xe2.innerText =" Amazheng "; // modify break; // can be used to find out.}} Break;}}
XMLDoc.save ("BookStore.xml"); // Save.
/ / =========================================================================================================================================================================================== ==
The last result is:
XML Version = "1.0" encoding = "GB2312"?>
XMLNodelist XNL = XmLDoc.selectsinglenode ("BookStore"). ChildNodes;
Foreach (XMLNode XN IN XNL) {XMLELEMENT XE = (XMLELEMENT) XN; IF (Xe.GetaTribute ("genre") == "fantasy" {xe.removeattribute ("genre"); // Delete Genre attribute} else IF (Xe.GetaTRibute ("genre") == "Update Li Zanli") {xe.removeall (); // Delete the entire content of the node}} Xmldoc.save ("BookStore.xml");
/ / =================================================== The final result is:
XML Version = "1.0" encoding = "GB2312"?>
4, display all data.
XMLNode XN = Xmldoc.selectsinglenode ("BookStore");
XMLNodelist XNL = xn.childNodes; Foreach (XMLNode XNF in XNL) {XMLELEMENT XE = (XMLELEMENT) XNF; Console.WriteLine (Xe.GetaTribute ("genre")); // Display attribute value console.writeline (Xe.GetaTRibute "ISBN")); XMLNodelist XNF1 = XE.ChildNodes; Foreach (XMLNode XN2 in Xnf1) {Console.WriteLine (xn2.innerText); // Display sub-node text}}