Solved

reading XML document forward and back

Posted on 2010-11-10
3
566 Views
Last Modified: 2012-05-10
I need to "seek" some nodes by name in an XML document, so I can retrieve the element value

XMLReader and XMLTextReader only read forward, since I don't know what order the nodes will be in within the XML I need to able to search forward and back or send the pointer back to the top so I can reissue a ReadToFollowing().

I don't want the overhead or opening and closing the reader every time, any ideas?

many thanks

Gary.
0
Comment
Question by:Gary2Seven
  • 2
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
For that kind of manipulation you wuold be better off loading into an XmlDocument and using XPath to query for nodes.
0
 
LVL 10

Expert Comment

by:P1ST0LPETE
Comment Utility
I agree with what carl said above.  Load the xml document into the XmlDocument class and use XPathNavigator to navigate through the document.  I use it ALL the time and love it.

Attached is a small example in C#:
//Import the "System.Xml" and "System.Xml.XPath" namespaces at the top of you're code.

public void ReadXml(string xmlFilePath)
{
     //Load xml file and create navigator:
     XmlReader reader = new XmlTextReader(xmlFilePath);
     XmlDocument doc = new XmlDocument();
     doc.Load(reader);
     reader.Close();
     XPathNavigator nav = doc.CreateNavigator();

     nav.MoveToRoot();  //Moves to the very beginning of the document (before any nodes)
     nav.MoveToFirstChild();  //Moves to the first child node of root, which is your main xml node

     //Then you can roll through chile nodes using loops like this:
     do
     {
          if(nav.Name == "SomeXmlTagName")
          {
               string mystring = nav.InnerXml;
          }
     }(nav.MoveToNext());
}

Open in new window

0
 
LVL 10

Accepted Solution

by:
P1ST0LPETE earned 250 total points
Comment Utility
Need to be more clear regarding my comments on these two lines:

nav.MoveToRoot();  //Moves to the very beginning of the document (before any nodes)
nav.MoveToFirstChild();  //Moves to the first child node of root, which is your main xml node

The method "MoveToFirstChild()" will move the xpath cursor to first child node of whatever node the xpath cursor is currently on (if there is a child to move too).  In the case above, since you are starting from "root" and using the MoveToFirstChild() method, then the cursor will be moved to your opening xml node.

For example, look at the following xml and code:

(root)
<people>
    <person>
        <firstname>Paul</firstname>
        <lastname>Johnson</lastname>
    </person>
    <person>
        <firstname>Steve</firstname>
        <lastname>Smith</lastname>
    </person>
</people>


nav.MoveToRoot();         //Moves to root
nav.MoveToFirstChild();  //Moves to <people>
nav.MoveToFirstChild();  //Moves to first <person>
nav.MoveToFirstChild();  //Moves to <firstname>Paul</firstname>
nav.MoveToNext();         //Moves to <lastname>Johnson</lastname>
nav.MoveToParent();      //Moves to first <person>
nav.MoveToNext();         //Moves to second <person>
nav.MoveToParent();      //Moves to <people>
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now