Karan's Blog

Just Microsoft .Net

Parse an XML file

The following code shows a simple method of parsing through an XML file/string. We can get the parent name, child name, attributes etc from the XML. The namespace System.Xml would be the only additional namespace that we would be using.

string myXMl = "<Employees>" +
"<Employee ID='1' Name='John Mayer'" +
"Address='12th Street'" +
"City='New York' Zip='10004'>" +
"</Employee>" +
"</Employees>";


XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(myXMl);

XmlNodeList xNodeList = xDoc.SelectNodes("Employees/child::node()");
foreach (XmlNode xNode in xNodeList)
{
if (xNode.Name == "Employee")
{
string ID = xNode.Attributes["ID"].Value;
//Outputs: 1
string Name = xNode.Attributes["Name"].Value;
//Outputs: John Mayer
string Address = xNode.Attributes["Address"].Value;
//Outputs: 12th Street
string City = xNode.Attributes["City"].Value;
//Outputs: New York
string Zip = xNode.Attributes["Zip"].Value;
//Outputs: 10004
}
}

Lets look at another XML:

string myXMl = "<root>" +
"<parent1>..some data</parent1>" +
"<parent2>" +
"<Child1 id='1' name='Adam'>data1</Child1>" +
"<Child2 id='2' name='Stanley'>data2</Child2>" +
"</parent2>" +
"</root>";
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(myXMl);
XmlNodeList xNodeList = xDoc.SelectNodes("root/child::node()");
//Traverse the entire XML nodes.
foreach (XmlNode xNode in xNodeList)
{
//Looks for any particular nodes
if (xNode.Name == "parent1")
{
//some traversing....
}
if (xNode.Name == "parent2")
{
//If the parent node has child nodes then
//traverse the child nodes
foreach (XmlNode xNode1 in xNode.ChildNodes)
{
string childNodeName = xNode1.Name;
//Ouputs: Child1
string childNodeData = xNode1.InnerText;
//Outputs: data1

//Loop through each attribute of the child nodes
foreach (XmlAttribute xAtt in xNode1.Attributes)
{
string attrName = xAtt.Name;
//Outputs: id
string attrValue = xAtt.Value;
//Outputs: 1
}
}
}
}

 

Posted: Apr 29 2010, 10:57 PM by karan@dotnet | with 12 comment(s) |
Filed under: ,

Comments

Dhananjay Goyani said:

Why so much of pain when you already have award winning LINQ to XML?

# April 30, 2010 2:29 AM

Pedro Sousa said:

@Dhananjay Goyani:

My thoughts exactly

# April 30, 2010 4:09 AM

karan@dotnet said:

Thanks for your suggestion.

Well I did't get a chance to work on with Linq. Am sure you could easily done the above with Linq but I have heard/read many performance related issues with Linq. Specifically with Linq to sql so I don't want to jump into Linq as yet. I have seen many people posting on asp.net forum asking how to parse a xml without Linq so I thought of posting this. I am sure there are many others way to do other than the one I posted above.

# April 30, 2010 11:11 AM

Twitter Trackbacks for Parse an XML file - Karan's Blog [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Parse an XML file - Karan's Blog         [asp.net]        on Topsy.com

# May 1, 2010 2:31 AM

Dhananjay Goyani said:

I haven't tested it by myself, but stackoverflow (by any measure, a large site) has no problem with plain LinqToSql.

www.codinghorror.com/.../compiled-or-bust.html

# May 7, 2010 3:50 AM

jam said:

did XMlNodeList is supported by silverlight enabled

website

# July 2, 2010 10:10 AM

Jam said:

The above said is not working when I tried with the

silverlight enabled wensite...Most of the things like XMLNodeList and XDoc.loadXml are not working in silverlight.pls help

# July 2, 2010 10:39 AM

karan@dotnet said:

I havent got a chance to work on silverlight as yet so cant give you a definate answer on this. Will update you if I find anything on this.

# July 7, 2010 10:51 AM

albuterol sulfate said:

Hi there, I newcomer your blog via Google while searching in the performance of senior aid for a generosity attack and your post looks non-standard real stimulating on me

# August 28, 2010 7:50 PM

albuterol sulfate said:

Hi there, I bring prevalent your blog via Google while searching in retribution for gold medal grant-in-aid representing a generosity attack and your execution looks acutely attractive on me

# August 29, 2010 12:18 AM

weblogs.asp.net said:

Parse an xml file.. Corking :)

# May 14, 2011 8:50 PM

weblogs.asp.net said:

Parse an xml file.. Super :)

# June 29, 2011 6:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)