XMLReader question

Here is the source code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace ConsoleApplication1
    class Program
        static void Main(string[] args)

        static void ReadXML()
            XmlReader reader = XmlReader.Create(@Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test1.xml");
            while (reader.Read())
                if (reader.NodeType == XmlNodeType.Text)

            reader = XmlReader.Create(@Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test1.xml");
                Console.WriteLine("read to following method value " + reader.ReadInnerXml());



Open in new window

XML file
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<chapters total="2">

Open in new window

Both sections print out the same thing, chapter1 and chapter2.

Why does the first section of the code have to use reader.Value and the second section has to use reader.ReadInnerXML? If I swap the two or use one of the two in both instances, it does not work. So they are not interchangeable
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Carl TawnSystems and Integration DeveloperCommented:
The difference is that ReadToFollowing() moves the pointer to the Element, which as far as the reader is concerned, is separate to the Content element.

So, in order to read the Value property after using ReadToFollowing(), you would need to call Read() again to move the pointer to the content. Like:
reader.ReadToFollowing("chapter");         // move to "chapter" element
reader.Read();                                           // advance to Text element of "chapter"

Console.WriteLine(reader.Value);            // read value of Text element

Open in new window

Alternatively, you can call ReadElementContentAsString() from the "chapter" element to achieve the same thing:
reader.ReadToFollowing("chapter");         // move to "chapter" element
Console.WriteLine(reader.ReadElementContentAsString());            // read value of element

Open in new window

BobHavertyComhAuthor Commented:
Hi Imran, thanks for your answer. Your links seem to explain how the XMLReader class works and how the Read method works, but I didn't see any explanation in your links as to when and why someone would use value to get the value and when and why someone should use ReadInnerXML instead. I understand what the Read Method and the Readtofollowing method are doing but I don't understand why the value in the element is accessed two different ways, depending upon which reading method is used.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

BobHavertyComhAuthor Commented:
Hi Carl, your answer seems to be close and you seem to understand my question.  Chapter is a text element, so after calling readtofollowing("chapter"), it is already on a text element. So why would I need to call read again to get to a text element before I can call read.value? And what is the key difference between the Read.Value and ReadInnerXML method? They both seem to do the same thing, assuming the element that has been read to, is a text element.
Carl TawnSystems and Integration DeveloperCommented:
>> Chapter is a text element, so after calling readtofollowing("chapter"), it is already on a text element

Not in XmlReader terms it isn't. The chapter "element" and the "text" of the chapter element are two separate entities. Internally the XmlReader is structuring the node more like:


The reason being that the content of the "chapter" element may not simply be text, it could be another element, or some other complex data type (CDATA for example). Hence the separation between what the element is and what it contains.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
First answer was correct, but second answer told me the key difference to understand and I saw this exact behavior when I ran it in the debugger, which I should have done initially. So the Read method will take you to the text node portion of an element node, while readtofollowing takes you to the element node itself, but not it's inner text node, and therefore needs to call ReadInnerXML to get the value from the text node portion of the element rather than merely calling .Value as it would be able to if the cursor were actually on the text node portion.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.