using System;
using System.Xml;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string strXML = @ "<xml> < Employee Name = ""
Robin Ahuja ""
City = ""
New Delhi "" > < Project first = ""
HIMS ""
second = ""
Investo ""
Third = ""
SUMMIT ""
Fourth = ""
MediaCare "" / > < Education > < Matric > 1992 < /Matric> < Inter > 1994 < /Inter> < Graduation > 1998 < /Graduation> < PG > 2005 < /PG> < /Education> < /Employee> < Employee Name = ""
Naresh Kumar ""
City = ""
Hyderabad "" > < Project first = ""
Finacle ""
second = ""
Invento ""
Third = ""
Ideathon ""
Fourth = ""
MCare "" / > < Education > < Matric > 2001 < /Matric> < Inter > 2003 < /Inter> < Graduation > 2005 < /Graduation> < /Education> < /Employee> < /xml>
";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
foreach(XmlNode node in doc.DocumentElement.ChildNodes)
{
Console.WriteLine("Outer Node Name is {0} and its contain " + node.OuterXml + "\r\n", node.Name);
foreach(XmlAttribute atr in node.Attributes)
{
Console.WriteLine("Attribute Name : {0} Value: {1} \r\n", atr.Name, atr.Value);
}
foreach(XmlNode innernode in node.ChildNodes)
{
Console.WriteLine("Inner Node 1 " + innernode.OuterXml + "\r\n");
foreach(XmlAttribute atr in innernode.Attributes)
{
Console.WriteLine("Attribute Name : {0} Value: {1} \r\n", atr.Name, atr.Value);
}
foreach(XmlNode lchild in innernode.ChildNodes)
{
Console.WriteLine("Inner Node 2 " + lchild.OuterXml + "\r\n");
Console.WriteLine("Inner Text : {0} \r\n", lchild.InnerText);
}
}
//2nd level
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(node.OuterXml);
foreach(XmlElement elmnt in doc2.DocumentElement)
{
Console.WriteLine("Node2 elements: " + elmnt.Name);
Console.WriteLine("Node2 elements: " + elmnt.NodeType);
}
}
Console.ReadLine();
}
}
}This code snippet for XML Parsing using XmlDocument API.