?
Solved

reading XML document forward and back

Posted on 2010-11-10
3
Medium Priority
?
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

777 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