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/">
<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>
就像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅我永远无法预测 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.