One XML file is known 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
/ / ================ 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 the genre attribute} else if (Xe.GetaTRibute ("genre") == "Update Li Zan Hong") {xe.removeall (); // Delete all the contents of the node}} Xmldoc.save ("BookStore.xml");
/ / =====================
The last 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")); // Displays the property value console.writeline ("ISBN"));
XMLNodelist XNF1 = XE.ChildNodes; Foreach (XMLNode XN2 in Xnf1) {Console.Writeline (xn2.innertext); // Display sub-node text}} other: use system; using system.io; using system.web; using Summary description of system.text; using system.collections; namespace shopWeb.class {///
XmlNode Parent = xmlLog.CreateElement (ParentName); XmlNode Child; IDictionaryEnumerator myEnumerator = HTChilds.GetEnumerator (); while (myEnumerator.MoveNext ()) {Child = xmlLog.CreateElement (myEnumerator.Key.ToString ()); Child.InnerText = Myenumerator.Value.toString (); parent.appendchild (child); xmllog.documentelement.Appendchild (Parent);
} XMLLog.save (Context.Server.mAppath (XMLPath)); Return True;} catch {return false;
}
} ///
}
} ///
} CatCh {return -1;
}}} /// XMLLog.Save (Context.Server.mAppath (XMLPath)); Return True; } Catch {return false;}} ///