Monday, March 21, 2011

Read and Update XML value using “SelectSingleNode” method.

Read and Update XML value using "SelectSingleNode" method.

There are multiple methods of reading XML values and lots of time we do
looping and reading node values one by one, I found very good of reading
xml node values directly by using proper node path. Assume you have xml 
file from where you have to read the values.
Read and Update node path will be changed based on the XML format. 
See some example below   

Sample XML 1 (File Name: "Sample.xml")

<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Employees DeptId="10">
              <Info name="Ritesh" Id="100" salary="20000"></Info>
              <Info name="Kumar" Id="200" salary="30000"></Info>
       </Employees>
       <Employees DeptId="20">
              <Info name="Kitesh" Id="100" salary="10000"></Info>
              <Info name="Gupta" Id="200" salary="20000"></Info>
       </Employees
</Root>

C# Code for Reading XML File

using System.Xml;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Sample.xml");

//[OPERATION]:Read Employee info that belongs to Department
Number 10 and Id is 100

XmlNode xmlNodeComponent = xmlDoc.SelectSingleNode("//Root/Employees[@DeptId='10']/Info[@Id='100']");

if (xmlNodeComponent != null)
{ 
string empName = xmlNodeComponent.Attributes["name"].Value;
// Ritesh
string empId = xmlNodeComponent.Attributes["Id"].Value;
// 100
string empSalary = xmlNodeComponent.Attributes["salary"].Value;
// 20000
}

Update XML value with new value

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);

XmlNode xmlNodeComponent =
xmlDoc.SelectSingleNode("//Root/Employees[@DeptId ='10']/Info[@Id='100']");

if (xmlNodeComponent != null)
{
//[OPERATION]: Update Employee salary who is belongs to Department 
Number 10 and Id is 100

  xmlNodeComponent.Attributes["salary"].Value = "60000";
  xmlDoc.Save(xmlFile);
}

Here another XML file sample with different type, the node path will be
different to read node value.
Sample XML 2 (File Name: "Sample.xml")

<?xml version="1.0" encoding="utf-8" ?>
<Root>
       <Employees DeptId="10" Id="100">
              <name>Ritesh Kumar</name>
              <salary>2000</salary>
              <DOB>23-Jun-1980</DOB>           
       </Employees>

       <Employees DeptId="20" Id="100">
              <name>Kitesh Gupta</name>
              <salary>3000</salary>
              <DOB>12-Mar-1980</DOB>
       </Employees>
</Root>

C# Code for Reading XML File

using System.Xml;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("Sample.xml");

//[OPERATION]:Read Employee info that is belongs to Department
Number 10 and Id is 100

XmlNode xmlNodeComponent =
xmlDoc.SelectSingleNode("//Root/Employees[@DeptId='10'][@Id='100']");

if (xmlNodeComponent != null)
{
string empName = xmlNodeComponent.ChildNodes[0].InnerText;
// Ritesh Kumar
string empSalary = xmlNodeComponent.ChildNodes[1].InnerText;
// 2000
string empDOB = xmlNodeComponent.ChildNodes[2].InnerText;
// 23-Jun-1980
}

Update XML value with new value

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFile);

XmlNode xmlNodeComponent = xmlDoc.SelectSingleNode(("//Root/Employees[@DeptId='10'][@Id='100']");

if (xmlNodeComponent != null)
{
//[OPERATION]: Update Employee salary who is belongs to Department
Number 10 and Id is 100
  xmlNodeComponent. ChildNodes[1].InnerText = "60000";
  xmlDoc.Save(xmlFile);
}

Some other links for basic XML Operation

No comments: