Solved

XMLReader Read verses ReadToFollow Method

Posted on 2014-01-01
4
1,505 Views
Last Modified: 2014-01-02
This code works.  It prints out two values from the xml file like it should
reader = XmlReader.Create("c:\\test.xml");
      while (reader.ReadToFollowing("chapter"))
      {
        Console.WriteLine(reader.ReadInnerXml());
      }
      reader.Close();

Open in new window


This code only prints the value once and does not print the second instance of the value of a second element with the same name which is in the XML file
reader = XmlReader.Create("c:\\test.xml");
      while (reader.Read())
      {
          reader.ReadToFollowing("chapter");
          Console.WriteLine(reader.ReadInnerXml());
      }
      reader.Close();

Open in new window


I thought that even though it might be an awkward way to do it, the example that doesn't work is walking through each node and then reading to the next chapter element. So why would this only get the first value but not eventually the second, even if in a non efficient manner? Seems like it has something to do with cursor position but I don't know exactly what.

How I see it is that it goes to the first node, and then goes to the chapter element and gets the value, and then the Read method would read the next node after the first chapter element that readtofollowing("chapter") took the cursor to, and then go to the second chapter element following the node that it is currently on, and then walk through any other nodes remaining and fail on an attempt of readtofollowing("chapter") because there would be no more chapter elements left.

So I am missing something very fundamental and important.
0
Comment
Question by:BobHavertyComh
[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
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39750895
You basically have this:

1) Read() call moves pointer to the "xml" directive
2) ReadToFollowing() call moves pointer to first "chapter" element
3) Read() call moves pointer to second "chapter" element
4) ReadToFollowing() finds nothing because it is searching from the current location, and doesn't include the current node.

So your problem is that the Read() in the loop is pushing the pointer past the second "chapter".
0
 
LVL 9

Author Comment

by:BobHavertyComh
ID: 39750968
Hi Carl, I understand your answer. Is this not working simply because the next node  just so happens to be another chapter element and that's why the Read method takes you to the next chapter element upon second iteration of the while? If there was only one chapter element in the chapters start element, and there was another chapter element in another start element such as "novel", would the Read method eventually get to that node via readtofiollowing within the while loop?

How I vision this is that in the first iteration of the while, the Read method takes me to the first node. Then the readtofollowing method within the while loop takes me to the chapters element after the first node. Then, on second iteration of the while loop, the Read method takes me to the next node after the first chapter element. So if the next node did not have a second chapter element, wouldn't it then go from the node it is at and find the second chapter element in the "novels" element through it's call to reaftofollowing within the while loop?

Your answer makes sense and I think I already know the answer to the second question I am asking, but I would like confirmation. Thanks.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39750985
All of the methods of the XmlReader operate from the node which the pointer currently points to. So, ReadToFollowing() literally means "move to the next element with this name that comes after the node i'm currently pointing at".

Read() on the other hand simply pushes the pointer to the next node in the document. So, if your XML looked like:
<chapters>
   <chapter>Chapter 1</chapter>
   <somethingelse />
   <chapter>Chapter 2</chapter>
</chapters>

Open in new window

Then the code you have would work fine because the logic would now be:

1) Read() call moves to XML declaration
2) ReadToFollowing() moves to first "chapter" element
3) Read() in the loop moves to the "somethingelse" element
4) ReadToFollowing() moves to the second "chapter" element
0
 
LVL 9

Author Closing Comment

by:BobHavertyComh
ID: 39751012
Ran this through the debugger. Seems like the difference is the following.

Since Read reads one node at a time, then just getting to the element node itself is not good enough to use the .Value property to get the text. The text portion of an element is a separate node from the element node itself. So Read is actually going to the text node for the element before asking for it's value, whereas Readtofollowing is merely going to the element node rather than the text node portion for that element, and therefore needs ReadInnerXML to get the text node portion of that element rather than using .Value because the cursor is not on that value after when using Readtofollowing.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

617 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