using System.Xml;
using System.Xml.XPath;
XPathNavigator nav;
XPathDocument docNav;
// Open the XML.
docNav = new XPathDocument(@"c:\books.xml");
// Create a navigator to query with XPath.
nav = docNav.CreateNavigator();
//Initial XPathNavigator to start at the root.
nav.MoveToRoot();
//Move to the first child node (comment field).
nav.MoveToFirstChild();
//Loop through all of the root nodes.
do {
} while (nav.MoveToNext());
do {
//Find the first element.
if (nav.NodeType == XPathNodeType.Element) {
//Determine whether children exist.
if (nav.HasChildren == true) {
//Move to the first child.
nav.MoveToFirstChild();
//Loop through all the children.
do {
//Display the data.
Console.Write("The XML string for this child ");
Console.WriteLine("is '{0}'", nav.Value);
} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
do {
//Find the first element.
if (nav.NodeType == XPathNodeType.Element) {
//if children exist
if (nav.HasChildren == true) {
//Move to the first child.
nav.MoveToFirstChild();
//Loop through all the children.
do {
//Display the data.
Console.Write("The XML string for this child ");
Console.WriteLine("is '{0}'", nav.Value);
//Check for attributes.
if (nav.HasAttributes == true) {
Console.WriteLine("This node has attributes");
}
} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
//Pause.
Console.ReadLine();
using System;
using System.Xml;
using System.Xml.XPath;
namespace q308343 {
class Class1 {
static void Main(string[] args) {
XPathNavigator nav;
XPathDocument docNav;
docNav = new XPathDocument(@"c:\books.xml");
nav = docNav.CreateNavigator();
nav.MoveToRoot();
//Move to the first child node (comment field).
nav.MoveToFirstChild();
do {
//Find the first element.
if (nav.NodeType == XPathNodeType.Element) {
//Determine whether children exist.
if (nav.HasChildren == true) {
//Move to the first child.
nav.MoveToFirstChild();
//Loop through all of the children.
do {
//Display the data.
Console.Write("The XML string for this child ");
Console.WriteLine("is '{0}'", nav.Value);
//Check for attributes.
if (nav.HasAttributes == true) {
Console.WriteLine("This node has attributes");
}
} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
//Pause.
Console.ReadLine();
}
}
}
docNav = new XPathDocument("c:\\books.xml");