Subscribe to Subscribers

Resources » .NET programming » .NET Framework

Parsing of XML data [DOM traversal]


Posted Date:     Category: .NET Framework    
Author: Member Level: Silver    Points: 7


This code snippet shows how to traverse the XML DOM and find out theattributes and values. This snippet uses the for(..) loop for traversal. The attribute ..Attributes.Item(0).. is the first attribute, ..Attributes.Item(1).. is the second attribute and so on...




XmlDocument xDoc=new XmlDocument(); //create an instance of the XML Document
try
{
xDoc.Load(path); //get the XML data from the file
XmlNode XNodeParentNode= xDoc.SelectSingleNode("//ParentNodeName"); //start - parsing the XML data //Get the parent Node

int NodeCnt=0;
int ChildNodeCnt=0;
int SuperChildNodeCnt=0;

XmlNode XNodeChildNode;
XmlNode XNodeSuperChildNode;
//traverse all the child nodes of the selected parent node
for(NodeCnt=0;NodeCnt<XNodeParentNode.ChildNodes.Count;NodeCnt++)
{
XNodeChildNode=XNodeParentNode.ChildNodes.Item(NodeCnt);
//this is the immediate child nodes
//do something
Response.Write(XNodeChildNode.Attributes.Item(2).Value);
//getting the attribute value of the child node at a given index
if(XNodeChildNode.Attributes.Item(3).Value=="0")
{
//checking for some value
XNodeSuperChildNode=XNodeChildNode.ChildNodes.Item(0);
//this is the second level child nodes of the selected parent node
if(XNodeSuperChildNode.Attributes.Item(3).Value!="1")
{
//do some thing
Response.Write("<font color=red>*</font>");
}
}
for(ChildNodeCnt=0;ChildNodeCnt<XNodeParentNode.ChildNodes.Count;ChildNodeCnt++)
{
XNodeSuperChildNode=XNodeChildNode.ChildNodes.Item(ChildNodeCnt);
//this is the superchild nodes of the selected parent node

//do something
Response.Write(XNodeSuperChildNode.Attributes.Item(2).Value);
}
}
}
catch(Exception ex)
{
//error trapping code
}






Did you like this resource? Share it with your friends and show your love!


Responses to "Parsing of XML data [DOM traversal]"
Author: critic    15 Jun 2004Member Level: Bronze   Points : 0
Article without proper description is not acceptible. If you are submitting code snippets, you must explain the code with proper description. Alos, the subtitle of the article is not descriptive.


Author: Debopriyo Das    05 Aug 2004Member Level: Bronze   Points : 0
The code snippet needs proper documentation. It seems to be incomplete code snippet. Any code presented should be complete, easy to understand and properly documented, so that any one can understand the program.


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Open File (database) using OpenFileDialog & Save (New) File using SaveFileDialog component
    Previous Resource: How to find difference between two Dates in C# or VB.NET ?
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.