I am using a very stupid method, but can help beginners understand the process of accessing the XML node. One XML file is known (bookstore.xml) is 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"); / / Create a node Xe1.setttribute ("Genre", "Li Zanhong"); // Set the Node Genre Attribute Xe1.Setattribute ("ISBN", "2-3631-4"); // Set the node ISBN Attribute 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.NNERTEXT = "58.3"; Xe1.Appendchild (Xesub3) Root.Appendchild (Xe1); // Add to Node XMLDoc.save ("BookStore.xml"); // ================ Result is:
XML Version = "1.0" encoding = "GB2312"?> Oberon's legacy title> Corets, EVA author> 5.95 price> book> CS from getting started to master title> 捷 author> 58.3 price> book> bookstore> 2, modify the node: Change the GENRE value of the GENRE attribute value "Le Li Zanhong" to "Update Li Zanli", the child of the node The text of the node is modified to "Ya Sheng". 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. 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> Sheng author> 58.3 price> book> bookstore> 3, delete Node GENRE attribute, delete node. XmlNodeList xnl = xmlDoc.SelectSingleNode ( "bookstore") ChildNodes;. Foreach (XmlNode xn in xnl) {XmlElement xe = (XmlElement) xn; if (xe.GetAttribute ( "genre") == "fantasy") {xe.RemoveAttribute ("genre"); // Delete the GENRE attribute} else if (xe.getattribute ("genre") == "Update Li Zanli") {xe.removeall (); // Delete all the contents of the node}} xmldoc.save ("BookStore.xml"); // ==================== The last result is:
XML Version = "1.0" encoding = "GB2312"?> Oberon's legacy title> corets, evA author> < Price> 5.95 price> book> book> bookstore> 4, display all data.