Modify the data in the XML document:
I. Use xpathnavigator to modify XML data
/// Obtain the DOM object
Xmldocument Doc = new xmldocument ();
// Load the XML document
Doc. Load (server. mappath ("~ // XML // mark. xml "));
// Obtain the xpathnavigator object
Xpathnavigator navigator = Doc. createnavigator ();
// Find the node whose ID attribute is equal to markvalue
String Path = @ "// * [@ ID = '" + markvalue + "']";
Foreach (xpathnavigator nav in navigator. Select (PATH ))
{
// Move to the first subnode of the current node
Nav. movetofirstchild ();
// Modify the value of the current node
Int mark = nav. valueasint;
Mark ++;
Nav. setvalue (mark. tostring ());
}
// Save the modification to the original file
Doc. Save (server. mappath ("~ // XML // mark. xml "));
Ii. Use Dom to modify XML data
// Differences between xmlelement and xmlnode:
// Xmlnode is an abstract class. xmlelement inherits from the xmlnode class.
// In an XML document,
// Each separate node is an xmlelement and an xmlnode
// But for xmlelement: it is a separate node with only "attributes"
// The sub-element does not exist.
// While xmlnode is in the big environment of "A Tree", it has its own attributes,
// There are also associated child nodes and parent nodes;
1. Use xmlelement
// Obtain the DOM object
Xmldocument Doc = new xmldocument ();
// Load the XML document
Doc. Load (server. mappath ("~ // XML // mark. xml "));
// Get the root node of the XML document
Xmlelement root = Doc. documentelement;
// Find the node whose ID attribute is equal to markvalue
String Path = @ "// * [@ ID = '" + markvalue + "']";
Foreach (xmlnode node in root. selectnodes (PATH ))
{
Xmlelement ele = node. firstchild as xmlelement;
Int mark = int. parse (Ele. innertext );
Mark ++;
Ele. value = mark. tostring ();
}
// Save the modification to the original file
Doc. Save (server. mappath ("~ // XML // mark. xml "));
2. Use xmlnode
// Obtain the DOM object
Xmldocument Doc = new xmldocument ();
// Load the XML document
Doc. Load (server. mappath ("~ // XML // mark. xml "));
// Get the root node of the XML document
Xmlelement root = Doc. documentelement;
// Find the node whose ID attribute is equal to markvalue
String Path = @ "// * [@ ID = '" + markvalue + "']";
Foreach (xmlnode node in root. selectnodes (PATH ))
{
// 1: Use the xmlnode object to find the required text data
// There are two methods:
// 1. search at the element node level: node. innertext
// 2. search at the text node level: node. value or node. innertext
// Use the Element Node Method
// If it is node. firstchild. value, its value will be equal to null.
// Because it actually represents its next-level subnode object: Text Node object
// Instead of a text value
// If you want to modify the text value of an element on an element node
// Use the node. firstchild. innertext attribute
Int mark = int. parse (node. firstchild. innertext );
Mark ++;
Node. firstchild. innertext = mark. tostring ();
// Use the text Node Method
// Locate the bottom of the node, that is, the text node.
// Node. value is the required text data.
Xmlnode xn = node. firstchild. firstchild;
Int mark = int. parse (Xn. value );
Mark ++;
XN. value = mark. tostring ();
}
// Save the modification to the original file
Doc. Save (server. mappath ("~ // XML // mark. xml "));