Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / May 2008

Tip: Looking for answers? Try searching our database.

Strange behaviour from XmlReader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jan Obrestad - 30 Apr 2008 15:55 GMT
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 - 30 Apr 2008 16:17 GMT
>             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.

>             int i = 0;
>             while (!(reader.NodeType == XmlNodeType.EndElement &&
[quoted text clipped - 9 lines]
>                 }
>             }

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().

Signature

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

Jan Obrestad - 02 May 2008 07:59 GMT
> As a solution you might want to use ReadString() instead of
> ReadElementString().

That fixed it.
Thank you!

Jan

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.