我从一个网络服务器的响应使用的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文件
发布评论