Private void btnload_click (Object Sender, System.EventArgs E)
{
XmlTextReader Reader = New XMLTextReader
Server.mappath ("mycompany.xml");
Reader.WhitespaceHandling = WhitespaceHandling.None;
XMLDocument Xmldoc = new xmldocument ();
// Load the file into the XMLDocument object
XMLDoc.Load (Reader);
// Close connection
Reader.Close ();
/ / Add an element that represents a document to ListBox
LBNODES.ITEMS.ADD ("XML Document");
/ / Find the root node and add it and its child node to listbox
XMLNode XNOD = XMLDoc.documenTelement;
AddwithChildren (XNOD, 1);
}
Private void addwithchildren (XMLNode XNOD, INT32 INTLEVEL)
{
// Add the node and its child node to listbox
// intLevel controls the depth of indentation
XMLNode Xnodworking;
String strindent = new string ('', 2 * intlevel);
// If the node value value, read its value
String strign = (string) xnod.value;
IF (Strvalue! = NULL)
{
Strval = ":" Strvalue;
}
// Add the details of the node to ListBox
LBNODES.ITEMS.ADD (Strindent XNOD.Name Strvalue);
// If it is an element node, get its properties
IF (XNOD.NODETYPE == xmlnodetype.element)
{
XMLNameDnodeMap MapAttributes = XNOD.Attributes;
// Add node properties to ListBox
Foreach (XMLnode Xnotattribute in MaPAttributes)
{
lbnodes.Items.add (Strindent " XNODATTRIBUTE.NAME
":" XNODATTRIBUTE.VALUE);
}
// If there is a child node, recursively call this program.
IF (XNOD.HASCHILDNODES)
{
XnodWorking = Xnod.firstchild;
While (xnodworking! = null)
{
AddwithChildren (Xnodworking, Intlevel 1);
Xnodworking = xnodworking.nextsibling;
}
}
}
}
}
/ Know one XML file (BookStore.xml) as follows: XML Version = "1.0" Encoding = "GB2312"?>
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
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"?>
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 last result is: XML Version = "1.0 "Encoding =" GB2312 ">>
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}}