StreamReader 并读取 XML 文件

发布于 2024-10-15 03:06:23 字数 582 浏览 5 评论 0原文

我使用 StreamReader 从 Web 服务器获得响应...现在我想解析此响应(它是一个 XML 文档文件)以获取其值,但每次我尝试执行此操作时都会收到错误:根元素丢失。

如果我直接读取同一个 XML 文件,则该文件格式良好,我可以读取它。

这是流:

WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
string responseString = responseReader.ReadToEnd();

这就是我尝试读取 XML 文件的方式:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(responseReader);
XmlNodeList address = xmlDoc.GetElementsByTagName("original");

I get a response from a web-server using StreamReader... now I want to parse this response (it's an XML document file) to get its values, but every time I try to do it I get a error: Root element is missing.

If I read the same XML file directly, the file is well formatted and I can read it.

This is the stream:

WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
string responseString = responseReader.ReadToEnd();

And this is how I try to read the XML file:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(responseReader);
XmlNodeList address = xmlDoc.GetElementsByTagName("original");

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

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

发布评论

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

评论(5

硬不硬你别怂 2024-10-22 03:06:23

您已调用ReadToEnd(),因此消耗了所有数据(进入字符串)。这意味着读者没有什么可以给予的。只是:不要那样做。或者,执行此操作并使用 LoadXml(reaponseString)

You have called ReadToEnd(), hence consumed all the data (into a string). This means the reader has nothing more to give. Just: don't do that. Or, do that and use LoadXml(reaponseString).

最初的梦 2024-10-22 03:06:23

Load 方法能够从远程资源获取 XML 文档。所以你可以像这样简化你的代码:

var xmlDoc = new XmlDocument();
xmlDoc.Load("http://example.com/foo.xml");
var address = xmlDoc.GetElementsByTagName("original");

不需要任何WebRequests、WebResponses、StreamReaders……(顺便说一句,你没有正确处理它们)。如果这不起作用,可能是因为远程 XML 文档不是真正的 XML 文档并且它已损坏。

The Load method is capable of fetching XML documents from remote resources. So you could simplify your code like this:

var xmlDoc = new XmlDocument();
xmlDoc.Load("http://example.com/foo.xml");
var address = xmlDoc.GetElementsByTagName("original");

No need of any WebRequests, WebResponses, StreamReaders, ... (which by the way you didn't properly dispose). If this doesn't work it's probably because the remote XML document is not a real XML document and it is broken.

雪化雨蝶 2024-10-22 03:06:23

如果您使用问题中粘贴的确切代码执行此操作,那么问题是您首先将整个流读入字符串,然后在调用时再次尝试读取流
<代码>
xmlDoc.Load(responseReader)

如果您已将整个流读取到字符串,请使用该字符串创建 xml 文档
<代码>
xmlDoc.Load(响应字符串)

If you do it with the exact code you pasted in your question, then the problem is that you first read the whole stream into string, and then try to read the stream again when calling

xmlDoc.Load(responseReader)

If you have already read the whole stream to the string, use that string to create the xml document

xmlDoc.Load(responseString)

默嘫て 2024-10-22 03:06:23

检查responseString 的内容是什么:可能它包含一些使 xmlparser 不满意的附加标头。

Check what's the content of responseString: probably it contains some additional headers that makes the xmlparser unhappy.

凉栀 2024-10-22 03:06:23

您收到的错误意味着您收到的 XML 缺少包装整个内容的第一个元素。尝试用一些元素包装您收到的答案,例如:

   WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
string responseString = responseReader.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXML( "<root>" + responseString + "</root>" );
XmlNodeList address = xmlDoc.GetElementsByTagName("original")

希望这有帮助

The error you are getting means, that XML you receive lacks first element that wraps the whole content. Try wrapping the answer you receive with some element, for example:

   WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader responseReader = new StreamReader(responseStream);
string responseString = responseReader.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXML( "<root>" + responseString + "</root>" );
XmlNodeList address = xmlDoc.GetElementsByTagName("original")

Hope this helped

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