Here is one way to do it.
Suppose the XML file looks like this:
person.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<person>
<name>Khelder</name>
<age>21</age>
</person>
<person>
<name>Redlehk</name>
<age>25</age>
</person>
</persons>
Then this will work:
Code:
using System;
using System.Xml;
namespace XmlTest
{
class XmlTest
{
[STAThread]
static void Main(string[] args)
{
// Load the Xml document
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("person.xml");
// Create an XmlTextWriter from the document
XmlTextWriter writer = new XmlTextWriter("person.xml", null);
writer.Formatting = Formatting.Indented;
// Loop through each person
XmlNodeList personList = xmlDoc.GetElementsByTagName("person");
for(int i = 0; i < personList.Count; i++)
{
XmlElement person = (XmlElement) personList[i];
// Get the person's name
XmlNodeList personNameList = person.GetElementsByTagName("name");
XmlElement personName = (XmlElement) personNameList[0];
// Get the person's age
XmlNodeList personAgeList = person.GetElementsByTagName("age");
XmlElement personAge = (XmlElement) personAgeList[0];
// Compare the data
if (personName.InnerText == "Redlehk")
personAge.InnerText = "44";
}
// Write the changes
xmlDoc.WriteContentTo(writer);
writer.Flush();
writer.Close();
}
}
}
Hope that helps.
David