One XML file is known as follows: XML Version = "1.0" encoding = "GB2312"?> Oberon's Legacy Title> Corets, EVA author> 5.95 price> book> bookstore> 1, insert a node into the node: xmlDocument Xmldoc = new xmldocument (); xmldoc.load ("bookstore.xml"); xmlnode root = xmldoc.selectsinglenode ("bookstore"); // Find Xmlelement XE1 = XmLDoc.createElement ("book"); // creation A Node Xe1.settribute ("Genre", "Li Zanhong"); // Set this node GENRE attribute XE1.SetAttribute ("ISBN", "2-3631-4"); // Setting this node ISBN property Xmlelement Xesub1 = xmldoc.createElement ("Title"); Xesub1.innertext = "CS From Getting Started to Wen"; // Settings Text Node XE1.AppendChild (Xesub1); // Add to Node XMLELEMENT XESUB2 = XMLDoc.createElement ("author"); Xesub2.innertext = "Waiting"; Xe1.Appendchild (Xesub2); XMlelement Xesub3 = XmLDoc.createElement ("Price"); XESUB3.INNERTEXT = "58.3"; Xe1.Appendchild (Xesub3); root .Appendchild (xe1); // Add to Node XMLDoc.save ("BookStore.xml"); / / =================== ============================ result is: XML Version = "1.0" encoding = "gb2312"> < Book genre = "fantasy" ISBN = "2-3631-4"> Oberon '
S Legacy title> Corets, EVA author> 5.95 price> book> cs from Getting Started to Jingjie Title> Waiting Author> 58.3 price> book> bookstore> 2, modify the node: Genre value of the GENRE attribute value is "Li Zanhong" Changed to "Update Li Zanli", modify the text of the child's child's child to "Yak". 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 property is "Update Li Zanli" 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 {xe2.innertext = "Assembly"; // modify break; // can be used to find out.}} Break;}} xmldoc.save ("BookStore. XML "); // Save.
/ / =========================================================================================================================================================================================== == The last result is: XML Version = "1.0" encoding = "GB2312"?> Oberon's legacy title> Corets, EVA author> 5.95 price> book> CS from getting started Title> Yaki author> 58.3 price> book> bookstore> 3, delete Node Genre Attribute, delete node.