为什么while循环没有返回任何内容?我认为这与.NET有关。我有.NET 2.0。问题是while语句将执行一次然后退出,就好像没有具有该名称的节点,当肯定存在时。
以下是XML的示例:
<rss version="2.0">
<channel>
<title>...</title>
<link>...</link>
<description>...</description>
<lastBuildDate>...</lastBuildDate>
<item>
<title>User greeting</title>
<guid>...</guid>
<link>http://...</link>
<description>Voicebox number: 1</description>
<author>Free Conference Call</author>
</item>
<item>
<title>User greeting</title>
<guid>...</guid>
<link>http://...</link>
<description>Voicebox number: 1</description>
<author>Free Conference Call</author>
</item>
</channel>
</rss>
以下是代码:
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create (reqURL);
// Set some reasonable limits on resources used by this request
webreq.MaximumAutomaticRedirections = 4;
webreq.MaximumResponseHeadersLength = 4;
//====================
// Set credentials to use for this request.
webreq.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)webreq.GetResponse ();
//====================
// Get the stream associated with the response.
receiveStream = response.GetResponseStream ();
//====================
// Pipes the stream to a higher level stream reader with the required encoding format.
readStream = new StreamReader(receiveStream, Encoding.UTF8);
resXML = readStream.ReadToEnd();
readStream = new StreamReader(receiveStream, Encoding.UTF8);
//====================
Console.WriteLine(resXML);
Stream XMLStream = receiveStream;
readStream = new StreamReader(XMLStream, Encoding.UTF8);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
XmlReader reader = System.Xml.XmlReader.Create(readStream, settings);
while(reader.EOF == false)
{
reader.ReadToFollowing("link");
Console.WriteLine("link: " + reader.Value);
//=======================================
reader.ReadToFollowing("description");
Console.WriteLine("descrip: " + reader.Value);
}
答案 0 :(得分:11)
调用
时,您已经阅读了响应中的所有数据resXML = readerStream.ReadToEnd();
... readerStream
和readStream
都位于receiveStream
之上,所以一旦您通过一个读取了所有数据,就无法再通过其他
对于响应,您应该有一个using
语句 - 理想情况下涉及的各种流。鉴于您可以从流中创建XmlReader
,而不仅仅是StreamReader
,为什么不使用这样的内容:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
using (HttpWebResponse response = (HttpWebResponse)webreq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using(XmlReader reader = XmlReader.Create(stream, settings))
{
// Do stuff here
}
}
}
此外,我通常建议创建一个XmlDocument
或类似的东西,而不是使用XmlReader
- 它不适用于大型文档,但它是更容易使用。
答案 1 :(得分:1)
为什么不使用Linq-to-XML?将使更易读/可维护的代码
XElement rssFile = XElement.Load(content);
IEnumerable<XElement> channels = rssFile.Descendants("channel");
foreach(XElement channel in channels)
{
Console.WriteLine("link: " + channel.Element("link").Value);
Console.WriteLine("description: " + channel.Element("description").Value);
}
除此之外,Jon说了什么
答案 2 :(得分:0)
像Jon说你已经阅读了所有数据,所以你不能再读它,但是,你应该能够将流的位置设置回零,然后再读一遍。
receiveStream.Position = 0;
我要说的另一个提示是将变量“readStream”重命名为“reader”或其他内容,因为它是StreamReader而不是Stream。
保重, 汤姆