Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

reading XML document forward and back

Posted on 2010-11-10
3
Medium Priority
?
574 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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