Load and Navigate elements in xml File using C#

I want to discuss an article how to Load and Navigate through XmlElements in XmlDocument.So You can load and Navigate or iterate through whole XmlDocument.There are many ways to navigate or iterate through xmlElements. I will describe how to do that with Code snippets and real time images.

sometimes you need to work with an xml file. XmlFile consists of Elements and Attributes.But to access them or navigate them through nodes in c#.We need to implement certain classes in c# code so that we can get or fetch the required xml Element or Attribute or Node in Xml File.So what are the classes we need to implement let us see in this Article....


To get a XMLdataDOCUMENT we need to import the Namespace Using System.xml(C#) or imports System.xml(vb.net)

Loading a Xml File in XMLdataDOCUMENT class

To load a Xml File in XMLdataDOCUMENT class the below code snippet will describe this

XmlDataDocument xxmldoc = new XmlDataDocument();
xxmldoc.Load(path of the File Name);


If you want to test whether the xml Elements are loaded in the xmldocument than check the innerxml of the xmldatadocument.

Debug with Xml

Now there is another method to load the xml Contents i.e

xmldoc.LoadXml(string xmlcontent)

XmlDataDocument xmldoc = new XmlDataDocument();
// xmldoc.Load("C:\\Users\\Bhushan\\Desktop\\installshield puporse\\WebSite1\\Bhushan.xml");

System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("(<) Root (>)");
sb.Append("(<) TextBook (>)");
sb.Append("C# With Linq");
sb.Append("Ronald Ross");
sb.Append(" 3.5");

To test whether the xml contents are loaded in XmldataDocument....



Before Navigating elements we need to know what is an xml Element and what is an xml Attribute and xml elemetnt is

name bHUSHAN name this is an xml Element
lastname id=2 lastname this xml attribute......

Now our next part is Navigating or iterating through xml Elements.

There are many ways to Navigate the Xml Nodes and Xml Attributes...






6)Through reading the xmlElements in dataset we can Read and navigate or iterate the xmlElements....


The Options are wide Open for us to iterate or Navigate the Xml Elements....

Going with Last Option

Dataset dst =new Dataset();
dst.readxml(path of the filename);
for(int i=0;i{
lblName.Text = dst.Tables[0].rows[i][0].Tostring();


The difference between Xmldoc.SelectNodes and Xmldoc.SelectSingleNode is that the xmldoc.SelectNodes will return xmlnodelist where as xmldoc.SelectSingleNode will return xmlnode.

The below is the Syntax for the Both Codes

xmlNodeList xmlnd = xmldoc.SelectNodes("/Root/TextBook");

xmlNode xmld = Xmldoc.SelectSingleNode("/Root/TextBook");

if You want to retrieve particular Node with Node Name and the required string we can do the following thing...

XmlNodeList lst = doc.SelectNodes("//INDIVIDUAL[FIRST_NAME[contains(., 'Bhushan')]]");

with Xpathnavigator we can use to navigate the Path through xmlElements and move through first child,Last Child,Parent and with Use of While Loop the general syntax
of the XpathNavigator...

XmlDocument document = new XmlDocument();
document.Load(path of the file);
XpathNavigator navigator = document.Navigator();
XPathNodeIterator nodes = navigator.Select("/bookstore/book");
while (nodes.MoveNext())

Here in the above the Create Navigator will create the path to navigate through xml elements....


Like this we can navigate through first child,Last Child and Move all the Xml Elements.....

for using xpathIterator we need to use xpathdocument to load the xml file....

xpathdocument xpathdoc = new xpathdocument(path of the file);
xpathNavigator xnav = xpathdoc.CreateNavigator();
XPathNodeIterator xPathIterator = xPathNavigator.Select("/bookstore//book");


xmlTextReader rdr =new XmlTextReader(path of the file name)
case XmlNodeType.Element
case XmlNodeType.Text


so these are the ways we can navigate through the xmldocuments....


Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 74 articles authored by srirama


No responses found. Be the first to 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: