Reading XML data in c#

Bageshkumarbagi
Posted by Bageshkumarbagi under C# category on | Points: 40 | Views : 820
**************************c# code**********************************

using System;
using System.Xml;

namespace ConsoleApplication1
{
class Program
{


static void Main(string[] args)
{
XmlDocument doc;
XmlElement root;
string PATH = @"G:\VC++\test.xml";
doc = new XmlDocument();
doc.Load(PATH);
string name;
string age;
string address;
int i = 0;
root = doc.DocumentElement;
XmlNodeList PersonList = doc.GetElementsByTagName("Person");
foreach (XmlNode node in PersonList)
{
XmlElement PersonElement = (XmlElement)node;
i++;
name = PersonElement.GetElementsByTagName("Name")[0].InnerText;
age = PersonElement.GetElementsByTagName("Age")[0].InnerText;
address = PersonElement.GetElementsByTagName("Address")[0].InnerText;
Console.WriteLine("************ Record {0} ************",i);
Console.WriteLine("Name {0}", name);
Console.WriteLine("Age {0}", age);
Console.WriteLine("Address {0}", address);
Console.WriteLine("**********************************");
}
Console.Read();

}
}
}

***************************************XML Data*****************************************
<?xml version="1.0" encoding="utf-8"?>
<Persones>
<Person>
<Name>Bagesh Kumar Singh</Name>
<Age>25</Age>
<Address>Pune</Address>
</Person>
<Person>
<Name>Rajesh Kumar Singh</Name>
<Age>27</Age>
<Address>Patna</Address>
</Person>
<Person>
<Name>Mahesh Kumar Singh</Name>
<Age>30</Age>
<Address>chennai</Address>
</Person>
</Persones>
*********************************************************************************************

Thanks,
Bagesh Kumar Singh

Comments or Responses

Login to post response