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"?>
book>
bookstore>
1. Insert a
XMLDocument Xmldoc = new xmldocument ();
XMLDoc.Load ("BookStore.xml");
XMLNode root = xmldoc.selectsinglenode ("bookstore"); // Find
XMLELEMENT XE1 = XMLDoc.createElement ("Book"); // Create a
Xe1.setttribute ("Genre", "Li Zanhong"); // Set this node GENRE attribute
Xe1.SetaTRibute ("ISBN", "2-3631-4"); // Setting this node ISBN property
XMLELEMENT XESUB1 = Xmldoc.createElement ("Title");
Xesub1.innerText = "CS from getting started to proficiency"; // Set text node
XE1.Appendchild (Xesub1); // Add to
XMLELEMENT XESUB2 = Xmldoc.createElement ("Author");
XESUB2.INNNERTEXT = "Waiting";
XE1.Appendchild (Xesub2);
XMLELEMENT XESUB3 = Xmldoc.createElement ("Price");
XESUB3.INNNERTEXT = "58.3";
Xe1.appendchild (Xesub3);
Root.Appendchild (Xe1); // Add to
XMLDoc.save ("BookStore.xml");
/ / =======================================================================================
The result is:
XML Version = "1.0" encoding = "GB2312"?>
book>
book>
bookstore>
2. Modify the node: Change the genre value of the GENRE attribute value to the node of "Li Zanhong" to "Update Li Zanli", and modify the text of the child's child's child
XMLNodelist nodelist = xmldoc.selectsinglenode ("bookstore"). ChildNodes; // Get all child nodes in the BookStore node
Foreach (XMLNode Xn In nodelist) // Traverse all child nodes
{
XMLELEMENT XE = (XMLELEMENT) XN; // Convert the child node type to XMLELEMENT type
IF (Xe.GetaTribute ("genre") == "Li Zanhong" // If the GENRE attribute value is "Li Zanhong"
{
Xe.setttribute ("Genre", "Update Li Zanli"); // Modify this property as "Update Li Zanli"
XMLNodelist NLS = Xe.childNodes; // Continue all child nodes of the XE child node
Foreach (XMLNode XN1 in NLS) // Traverse
{
XMLELEMENT XE2 = (XMLELEMENT) XN1; // Conversion Type
IF (xe2.name == "author") // If you find
{
Xe2.innertext = "Assembly"; // modified
Break; // Find out to exit it.
}
}
Break;
}
}
XMLDoc.save ("BookStore.xml"); // Save.
/ / =========================================================================================================================================================================================== ==
The last result is:
XML Version = "1.0" encoding = "GB2312"?>
book>
book>
bookstore>
3, delete
XMLNodelist XNL = XmLDoc.selectsinglenode ("BookStore"). ChildNodes;
Foreach (XMLNode Xn In XNL)
{
XMLELEMENT XE = (XMLELEMENT) XN;
IF (Xe.GetaTribute ("genre") == "fantasy")
{
Xe.Removettribute ("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"?>
book>
book>
bookstore>
4, display all data.
XMLNode XN = Xmldoc.selectsinglenode ("BookStore");
XMLnodelist XNL = xn.childnodes;
Foreach (XMLNode Xnf in XL)
{
XMLELEMENT XE = (XMLELEMENT) XNF;
Console.WriteLine (Xe.GetaTribute ("genre"); // Displays property values
Console.writeline (Xe.GetaTRibute ("ISBN"));
XMLNodelist XNF1 = Xe.childNodes;
Foreach (XMLNode XN2 in Xnf1)
{
Console.writeline (xn2.innertext); // Displays subtitle text
}
}