Solved

reading XML document forward and back

Posted on 2010-11-10
3
570 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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