Get Node information by using XPathNavigator : XmlPathNavigator « XML « C# / CSharp Tutorial






using System;
using System.Xml.XPath;
using System.Xml;

class MainClass
{
  static void Main(string[] args)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"c:\books.xml");
    
        XPathNavigator nav = xmlDoc.CreateNavigator();

        nav.MoveToRoot();
        string name = nav.Name;
        Console.WriteLine("Root node info: ");
        Console.WriteLine("Base URI" + nav.BaseURI.ToString());
        Console.WriteLine("Name: " +nav.Name.ToString());
        Console.WriteLine("Node Type: "+ nav.NodeType.ToString());
        Console.WriteLine("Node Value: "+nav.Value.ToString());
    
        if (nav.HasChildren)
        {
            nav.MoveToFirstChild();
            GetNodeInfo(nav);
        }
    }

    private static void GetNodeInfo( XPathNavigator nav1)
    {
        Console.WriteLine("Name: " +nav1.Name.ToString());
        Console.WriteLine("Node Type: "+ nav1.NodeType.ToString());
        Console.WriteLine("Node Value: "+nav1.Value.ToString());
    
        if (nav1.HasChildren)
        {
            nav1.MoveToFirstChild();
            
            while( nav1.MoveToNext() )
            {
                GetNodeInfo(nav1);
                nav1.MoveToParent();
            }               
        } else {   
            nav1.MoveToNext();
            GetNodeInfo(nav1);
        }
    }
}








30.23.XmlPathNavigator
30.23.1.XPathNavigator: XPathNodeIterator with comparison
30.23.2.XPathNavigator: Evaluate
30.23.3.Create XPathNavigator object by calling CreateNavigator of XmlDocument
30.23.4.XPathNodeIterator from a select-node statement
30.23.5.Select a node
30.23.6.Get Node information by using XPathNavigator
30.23.7.Move to a child with XPathNavigator
30.23.8.Fast select by ID using XPathNavigator API