Asp.net modify data in XML documents

Source: Internet
Author: User

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 "));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.