Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Strange behaviour from XmlReader

Question posted by: Jan Obrestad (Guest) on June 27th, 2008 07:20 PM
Hello,

I've been using the XmlReader class to read xml files.
Lately it has had some strange quirks. It seems to ignore elements when
there is no whitespace between them.

ex
<main><textline><textelement>A</textelement>
<textelement>B</textelement<textelement>C</textelement></textline></main>
(really on one line)

works fine, but with

<main><textline><textelement>A</textelement><textelement>B</textelement><textelement>C</textelement></textline></main>

it skips element B.

and with
<main><textline><textelement>A</textelement>
<textelement>B</textelement><textelement>C</textelement></textline></main>

it skips element C

A short program to demonstrate the problem:

using System;
using System.Xml;
using System.IO;

namespace XmlTest
{
class TestXml
{
public static void Main()
{
string xml =
"<main><textline><textelement>A</textelement><textelement>B</textelement><textelement>C</textelement></textline></main>";
StringReader strReader = new StringReader(xml);
XmlReader reader = XmlReader.Create(strReader);
reader.MoveToContent();
int i = 0;
while (!(reader.NodeType == XmlNodeType.EndElement &&
reader.Name.Equals("textline")))
{
reader.Read();
if (reader.NodeType == XmlNodeType.Element &&
reader.Name.Equals("textelement"))
{
string text = reader.ReadElementString();
Console.WriteLine("{0} {1}", i, text);
i++;
}
}
Console.ReadLine();
}
}
}

This now prints
0 A
1 C

It should have printed
0 A
1 B
2 C

Does anyone have any idea what I might be doing wrong?
I had always thought that whitespace was irrelevant in xml.


Jan Obrestad

Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:20 PM
#2

Re: Strange behaviour from XmlReader
Jan Obrestad wrote:
Quote:
string xml =
"<main><textline><textelement>A</textelement><textelement>B</textelement><textelement>C</textelement></textline></main>";
>
StringReader strReader = new StringReader(xml);
XmlReader reader = XmlReader.Create(strReader);
reader.MoveToContent();


Now the reader is positioned on the 'main' start tag.
Quote:
int i = 0;
while (!(reader.NodeType == XmlNodeType.EndElement &&
reader.Name.Equals("textline")))
{
reader.Read();
if (reader.NodeType == XmlNodeType.Element &&
reader.Name.Equals("textelement"))
{
string text = reader.ReadElementString();
Console.WriteLine("{0} {1}", i, text);
i++;
}
}


For the first run of the loop:
reader.Read() moves reader to the 'textline' start tag.
On the second run of the loop:
reader.Read() moves reader to the 'textelement' start tag.
reader.ReadElementString() moves reader past the 'textelement' end
tag, meaning it is now positioned on the second 'textelement' start tag.
Output: i as 0, text as 'A'.
i set to 1
On the third run of the loop:
reader.Read() moves reader to the text node with contents 'B'.
So there you have the problem, your combination of
Read/ReadElementString and your conditions fail to find the second
'textelement' start tag.

As a solution you might want to use ReadString() instead of
ReadElementString().


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Jan Obrestad's Avatar
Jan Obrestad
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: Strange behaviour from XmlReader
Martin Honnen wrote:
Quote:
As a solution you might want to use ReadString() instead of
ReadElementString().


That fixed it.
Thank you!

Jan

 
Not the answer you were looking for? Post your question . . .
189,799 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors