.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Reading a Document using XpathNavigator

Posted By:Rajshree       Posted Date: September 15, 2010    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

Read a document using XpathNavigator There are many methods and properties which helps in navigating XML Document
 

static void Main(string[] args)
        {
           
// Load books.xml document
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"c:\books.xml");

           
// Create XPathNavigator object by calling CreateNavigator of XmlDocument
            XPathNavigator nav = xmlDoc.CreateNavigator();

           
// move to root node
            nav.MoveToRoot();
            string name = nav.Name;
            Console.WriteLine("Root node info: ");
            Console.WriteLine("Base URI" + nav.BaseURI.ToString());
            Console.WriteLine("Name: " + nav.NodeType.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 node has children, move to first child.
            if (nav1.HasChildren)
            {
                nav1.MoveToFirstChild();
                while (nav1.MoveToNext())
                {
                    GetNodeInfo(nav1);
                    nav1.MoveToParent();
                }
            }

            else
/* Else move to next sibling */
            {
                nav1.MoveToNext();
                GetNodeInfo(nav1);
            }
        }


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend