Thursday, February 7, 2008

How to access XPathNavigator using XML DOM

After create an xmldoument, you might need to access the particular node, xpath enables you

access the node or set of nodes in an xml document. xpath treats xml document as a tree

structure.

xpath implementation recognize the several node types in the xml document, such as root, node, element, attribute, comment, white space, significant whitespace, namespace,

Xpath class defined in the system.xml.path, Xpath navigator can access any of the datasource , such as Dataset, database, xml document.

The below code describe , how to acces the xpathnavigator class.

XPathDocument Doc = new XPathDocument("emp.xml");
XPathNavigator navigator = Doc.CreateNavigator();
XPathNodeIterator iterator = navigator.Select("/employees/employee");
while (iterator.MoveNext())
{
Console.WriteLine(iterator.Current.Name);
Console.WriteLine(iterator.Current.Value);
}