XMLTextReader 未读取所有元素

发布于 2024-12-04 20:40:48 字数 3264 浏览 0 评论 0原文

我正在尝试从 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 语法正在转换为其实体?

知道为什么会发生这种情况/如何解决它吗?

I am trying to read attributes from an XML stream.

Here is an example of the XML code

<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>

The code I am using to read is as follows:

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();
}

However, it only prints out

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

Any idea why the first element is considered the only "element" in the XML.

Thanks!

EDIT
When i covert the stream to a string using the following code:

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

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

I receive this text:

<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="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" />
<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" />
<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" />
</catalog>
</string>

Like the xml syntax is being converted to its entity?

Any idea why this is happening/how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

归途 2024-12-11 20:40:48

请参阅我永远无法预测 XMLReader 的行为。有什么理解技巧吗?

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

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

See I can never predict XMLReader behavior. Any tips on understanding?

I suggest avoiding Move methods, which will probably improve your reader's behavior.

Edit: However, I have tested your code and it prints all the attributes for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文