简体   繁体   English

将响应流加载到 XDocument 中,缺少根元素

[英]loading a responsestream into an XDocument, root element is missing

I have read that setting the position of the stream to 0 resolves this, but this fails as the stream "does not support seek operations".我已阅读将 stream 的 position 设置为 0 可以解决此问题,但这会失败,因为 stream“不支持查找操作”。

It fails on this:它失败了:

XDocument doc = XDocument.Load(resp.GetResponseStream());

Reading the stream:读取 stream:

string t = new StreamReader(resp.GetResponseStream(), Encoding.Default).ReadToEnd();

...reveals that my xml could not be any simpler: ...显示我的 xml 再简单不过了:

<xml version="1.0">
  <ActiveStorms>
  </ActiveStorms>
</xml>

Is this somehow malformed?这是某种格式不正确的吗?

Thanks for any help, Mike感谢您的帮助,迈克

XML documents do not end with a </xml> closing tag so delete that. XML 文档不以</xml>结束标记结束,因此请将其删除。 The initial <xml version="1.0"> should be: <?xml version="1.0"> (note the question mark).初始<xml version="1.0">应该是: <?xml version="1.0"> (注意问号)。

So a valid version would look like:所以一个有效的版本看起来像:

<?xml version="1.0">
<ActiveStorms>
</ActiveStorms>

The right XML declaration is正确的 XML 声明是

<?xml version="1.0" encoding="utf-8" ?>

and after that add your root node <ActiveStorms> so,然后添加你的根节点<ActiveStorms>所以,

<?xml version="1.0" encoding="utf-8" ?>
<ActiveStorms>
</ActiveStorms>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM