Recently, XML files have been used in the project. You need to save some configuration information to the specified XML file. Therefore, the function of updating node values in XML files is used.
First, create an XML file and create several values in the file. Let's take a look at the demo'sCode:
Private createxmlfile ()
{
// Create the xmldocument.
Xmldocument Doc = new xmldocument ();
Doc. loadxml ("<Category> <Name> Kevin </Name> </Category>");
// Add a price element.
Xmlelement newelem = Doc. createelement ("mobilephone ");
Newelem. innertext = "Nokia ";
Doc. documentelement. appendchild (newelem );
// Save the document to a file. white space is
// Preserved (no white space ).
Doc. preservewhitespace = true;
Doc. Save ("data. xml ");
}
The following describes how to update the node values in an XML file. The Code is as follows:
Private void updatexmlnodevalue ()
{
String strxmlpath = application. startuppath + "\ data. xml ";
Doc. Load (strxmlpath );
Xe = Doc. firstchild as xmlelement;
Xmlnode xnrc = Xe. selectsinglenode ("mobilephone ");
If (xnrc! = NULL)
{
Xnrc. innertext = "apple ";
Doc. Save (strxmlpath );
}
}