开发者

XMLReader failing when there are no return characters

I am using XML reader class and I have made the folowing example.

        XmlReader xmlreader = XmlReader.Create("http://adomain/companies.xml");

        while (xmlreader.ReadToFollowing("company"))
        {
            Console.WriteLine("Company {0}",xmlreader.ReadElementContentAsString());

            if (xmlreader.ReadToFollowing("employees"))
            {
                Console.WriteLine("Employees{0}", xmlreader.ReadElementContentAsString());
            }
            Console.WriteLine();
        }

The code works fine when the XML has carriage returns results like the following

Company Web Spiders Co Employees20

Company Google Employees20000

Company Simons Gremlin Web Design Employees1

with xml like this

<?xml version="1.0" encoding="UTF-8"?>

<companies>

<company>Web Spiders Co</company>

<employees>20</employees>

<company>Google</company>

<employees>20000</employees>

<company>Simons Gremlin Web Design</company>

<employees>1</employees>

</companies>

however when I remove the linebreaks from the XML

i.e. <?xml version="1.0" encoding="UTF-8"?> <companies> <company>Web Spiders Co</company> <employees>20</employees> <company>Google</company> <employees>20000</employees> <开发者_运维百科;company>Simons Gremlin Web Design</company> <employees>1</employees> </companies>

then the xmlreader starts to skip over elements and my result set looks very different

e.g.

Company Web Spiders Co Employees20000

I don't really understand why this is happening, and why the XML reader behaves in this way.


This is failing because when you have the newlines, there is an extra node of type Whitespace, which makes your logic work correctly. Without the newlines and these extra nodes seen by the reader, the auto-skipping to next element done by the ReadElementContentAsString messes things up.

This works for both - here, you check whether you are already at the desired spot before getting the element content, and only move if you are not already there.

XmlReader xmlreader = XmlReader.Create(@"..\..\test.xml.txt");

while ((xmlreader.Name == "company") || xmlreader.ReadToFollowing("company"))
{
    Console.WriteLine("Company {0}", xmlreader.ReadElementContentAsString());

    if ((xmlreader.Name == "employees") || xmlreader.ReadToFollowing("employees"))
    {
        Console.WriteLine("Employees{0}", xmlreader.ReadElementContentAsString());
    }
    Console.WriteLine();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜