XPathNavigator.SelectAncestors selects all the ancestor nodes of the current
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml.XPath;
public class MainClass
{
public static void Main()
{
XPathDocument document = new XPathDocument("domainBooks.xml");
XPathNavigator navigator = document.CreateNavigator();
navigator.MoveToChild("bookstore", "http://www.domain.com/books");
navigator.MoveToChild("book", "http://www.domain.com/books");
XPathNodeIterator bookDescendants = navigator.SelectDescendants("", "http://www.domain.com/books", false);
while (bookDescendants.MoveNext())
{
Console.WriteLine(bookDescendants.Current.Name);
}
XPathNodeIterator bookChildren = navigator.SelectChildren("", "http://www.domain.com/books");
while (bookChildren.MoveNext())
{
Console.WriteLine(bookChildren.Current.Name);
}
navigator.MoveToChild("title", "http://www.domain.com/books");
XPathNodeIterator bookAncestors = navigator.SelectAncestors("", "http://www.domain.com/books", false);
while (bookAncestors.MoveNext())
{
Console.WriteLine(bookAncestors.Current.Name);
}
}
}
Related examples in the same category