的StreamReader和读取XML文件

编程入门 行业动态 更新时间:2024-10-25 09:28:03
本文介绍了的StreamReader和读取XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从一个网络服务器的响应使用的StreamReader ......现在我要分析此响应(它是一个XML文档文件)来获得它的值,但每次我尝试这样做,我得到一个错误:根元素是缺少。

如果我直接读取同一个XML文件,该文件是格式正确,我可以读取它。

这是流:

WebResponse的响应= webRequest.GetResponse(); 流responseStream = response.GetResponseStream(); StreamReader的responseReader =新的StreamReader(responseStream); 字符串responseString = responseReader.ReadToEnd();

这是我尝试读取XML文件:

的XmlDocument xmlDoc中=新的XmlDocument(); xmlDoc.Load(responseReader); XmlNodeList中地址= xmlDoc.GetElementsByTagName(原始);

解决方案

您已经叫 ReadToEnd(),因此占用了所有的数据(转换成字符串)。这意味着读者有什么更多的付出。只是:不这样做。或者说,做到这一点,使用的loadXML(reaponseString)。

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");

解决方案

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).

更多推荐

的StreamReader和读取XML文件

本文发布于:2023-11-12 07:18:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580864.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   StreamReader   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!