Copy codeThe Code is as follows:
Using System. Xml;
// Initialize an xml instance
XmlDocument xml = new XmlDocument ();
// Import the specified xml file
Xml. Load (path );
Xml. Load (HttpContext. Current. Server. MapPath ("~ /File/bookstore. xml "));
// Specify a node
XmlNode root = xml. SelectSingleNode ("/root ");
// Obtain all direct subnodes under a node
XmlNodeList childlist = root. ChildNodes;
// Determine whether a subnode exists under the node
Root. HasChildNodes;
// Obtain the set of nodes with the same name as the peer node
XmlNodeList nodelist = xml. SelectNodes ("/Root/News ");
// Generate a new node
XmlElement node = xml. CreateElement ("News ");
// Add a node to a specified node as its subnode
Root. AppendChild (node );
// Add the node to a subnode under the specified Node
Root. InsertBefore (node, root. ChildeNodes [I]);
// Create a property for the specified node and assign a value to it
Node. SetAttribute ("id", "11111 ");
// Add a subnode to a specified Node
Root. AppendChild (node );
// Obtain the specified attribute value of a specified Node
String id = node. Attributes ["id"]. Value;
// Obtain the text in the specified Node
String content = node. InnerText;
// Save the XML file
String path = Server. MapPath ("~ /File/bookstore. xml ");
Xml. Save (path );
// Or use: xml. Save (HttpContext. Current. Server. MapPath ("~ /File/bookstore. xml "));