XMLTextReader不读取所有元素

时间:2011-09-16 16:29:36

标签: c# xml xmltextreader

我正在尝试从XML流中读取属性。

以下是XML代码的示例

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<catalog version="1.1">
<dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX4" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
<dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX4" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
<dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX4" format="XXX" starttime="XXX" endtime="XXX" extentleft="XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" />
</catalog>
</string>

我用来阅读的代码如下:

XmlTextReader readerXML = new XmlTextReader(responseStream);

while (readerXML.Read())
{
if (readerXML.HasAttributes)
{
    Console.WriteLine(readerXML.Name + " Attribute");
    for (int i = 0; i < readerXML.AttributeCount; i++)
    {
        readerXML.MoveToAttribute(i);
        Console.WriteLine("Name: " + readerXML.Name + ", Value: " + readerXML.Value);
    }
}
readerXML.MoveToElement();
}

但是,它只打印出来

string Attribute
Name: xmlns, Value: http://schemas.microsoft.com/2003/10/Serialization/

任何想法为什么第一个元素被认为是XML中唯一的“元素”。

谢谢!

修改 当我使用以下代码将流转换为字符串时:

            WebResponse response = restWebRequest.GetResponse();
            Stream responseStream = response.GetResponseStream();

            StreamReader reader = new StreamReader(responseStream);
            string responseFromServer = reader.ReadToEnd();
            textBox1.Text = responseFromServer;

我收到了这封文:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
&lt;catalog version="1.1"&gt;
&lt;dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="-XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" /&gt;
&lt;dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="-XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" /&gt;
&lt;dataset id="354" name="XXX" description="XXX" datatype="XXX" rank="XXX" saropsrank="XXX" format="XXX" starttime="XXX" endtime="XXX" extentleft="-XXX" extentbottom="XXX" extentright="XXX" extenttop="XXX" source="XXX" wmslayeridstr="XXX" confidence="XXX" directionfrom="XXX" image="XXX" /&gt;
&lt;/catalog&gt;
</string>

就像xml语法正在转换为它的实体一样?

知道为什么会发生这种情况/如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

请参阅I can never predict XMLReader behavior. Any tips on understanding?

我建议避免使用Move方法,这可能会改善读者的行为。

编辑但是,我已经测试了您的代码并为我打印了所有属性。