Solved

reading XML document forward and back

Posted on 2010-11-10
3
568 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
ID: 34103043
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
ID: 34103290
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
ID: 34103417
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 104
Wav problem 4 25
Question! 4 36
.NET 2008 VB and C# 6 27
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

19 Experts available now in Live!

Get 1:1 Help Now