为什么 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);
}