StreamReader 并读取 XML 文件
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您已调用
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 useLoadXml(reaponseString)
.Load 方法能够从远程资源获取 XML 文档。所以你可以像这样简化你的代码:
不需要任何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:
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.
如果您使用问题中粘贴的确切代码执行此操作,那么问题是您首先将整个流读入字符串,然后在调用时再次尝试读取流
<代码>
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)
检查responseString 的内容是什么:可能它包含一些使 xmlparser 不满意的附加标头。
Check what's the content of responseString: probably it contains some additional headers that makes the xmlparser unhappy.
您收到的错误意味着您收到的 XML 缺少包装整个内容的第一个元素。尝试用一些元素包装您收到的答案,例如:
希望这有帮助
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:
Hope this helped