?
Solved

XMLReader question

Posted on 2014-01-01
6
Medium Priority
?
392 Views
Last Modified: 2014-01-02
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)
        {
            ReadXML();
            Console.ReadLine();
        }

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

            reader = XmlReader.Create(@Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test1.xml");
            while(reader.ReadToFollowing("chapter"))
            {
                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">
  <chapter>chapter1</chapter>
  <chapter>chapter2</chapter>
</chapters>

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
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
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 39750846
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 2000 total points
ID: 39750904
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

0
 
LVL 9

Author Comment

by:BobHavertyComh
ID: 39750930
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Author Comment

by:BobHavertyComh
ID: 39750952
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.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 39750963
>> 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:

<chapter>
    <text>chapter1</text>
</chapter>

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.
0
 
LVL 9

Author Closing Comment

by:BobHavertyComh
ID: 39751044
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.
0

Featured Post

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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