我正在尝试从 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 语法被转换成它的实体一样?
知道为什么会发生这种情况/如何解决吗?