为什么跳过的XmlReader所有其他元素,如果没有空格分隔符? [英] Why does XmlReader skip every other element if there is no whitespace separator?

查看:298
本文介绍了为什么跳过的XmlReader所有其他元素,如果没有空格分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到奇怪的行为,当我尝试使用LINQ XmlReader类来解析XML。下面的测试案例:它看起来像我是使用(的XElement)XNode.ReadFrom(的XmlReader)阅读()方法,它忽略输入XML第二元素。 /条&GT; &LT;酒吧&GT; 那么它将解析第二个<如果有任何空格在℃之间加code>栏元素正确。

有没有人有,为什么输入流得到弄糟,以及如何的想法来解决这个问题呢?

  [测试]
    [明确]
    公共无效ShouldParseCorrectNumberOfElements()
    {
        VAR XML = @&LT;富&GT;&LT;酒吧GT;跆拳道&LT; /条&GT;&LT;酒吧GT; wtf2&LT; /条&GT;&LT; /富&gt;中;
        的XmlReader的XmlReader = XmlReader.Create(新的MemoryStream(Encoding.UTF8.GetBytes(XML)));        诠释计数= 0;
        xmlReader.MoveToContent();
        而(xmlReader.Read())
        {
            如果(xmlReader.NodeType == XmlNodeType.Element&放大器;&安培; xmlReader.Name ==栏)
            {
                VAR元= xmlReader.ReadOuterXml();
                Console.WriteLine(只是得到了一个+元素);
                算上++;
            }
        }
        Assert.AreEqual(2,计数);
    }


解决方案

您正在呼叫 ReadOuterXml ,这将消耗元素,并将光标之前刚下一个元素。然后你打电话再次,它移动光标(例如文本节点元素中)。

下面是你的循环替代:

 而(!xmlReader.EOF)
{
    Console.WriteLine(xmlReader.NodeType);
    如果(xmlReader.NodeType == XmlNodeType.Element&放大器;&安培; xmlReader.Name ==栏)
    {
        VAR元= xmlReader.ReadOuterXml();
        Console.WriteLine(只是得到了一个+元素);
        算上++;
    }
    其他
    {
        xmlReader.Read();
    }
}

I'm seeing strange behavior when I try to parse XML using the LINQ XmlReader class. Test case below: it looks like whether I use (XElement)XNode.ReadFrom(xmlReader) or one of the Read() methods on XmlReader, it misses the second bar elements in the input XML. If any whitespace is added between the </bar> and <bar> then it will parse the second bar element correctly.

Does anyone have an idea of why the input stream gets messed up and how to get around this problem?

    [Test]
    [Explicit]
    public void ShouldParseCorrectNumberOfElements()
    {
        var xml = @"<foo><bar>wtf</bar><bar>wtf2</bar></foo>";
        XmlReader xmlReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(xml)));

        int count = 0;
        xmlReader.MoveToContent();
        while (xmlReader.Read())
        {
            if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "bar")
            {
                var element = xmlReader.ReadOuterXml();
                Console.WriteLine("just got an " + element);
                count++;
            }
        }
        Assert.AreEqual(2, count);
    }

解决方案

You're calling ReadOuterXml, which will consume the element and place the "cursor" just before the next element. You're then calling Read again, which moves the cursor on (e.g. to the text node within the element).

Here's an alternative to your loop:

while (!xmlReader.EOF)
{
    Console.WriteLine(xmlReader.NodeType);
    if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.Name == "bar")
    {
        var element = xmlReader.ReadOuterXml();
        Console.WriteLine("just got an " + element);
        count++;                
    }
    else
    {
        xmlReader.Read();
    }
}

这篇关于为什么跳过的XmlReader所有其他元素,如果没有空格分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆