你可以像这样获取这个文件:
private string fetchRSS(string url, DateTime lastUpdate){
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.IfModifiedSince = lastUpdate;
// if the file wasn't modified since the specified time it won't be fetched and an exception will be thrown
try{
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
return reader.ReadToEnd();
}
// this will happen if the fetching of the file failed or the file wasn't updated
return null;
}
然后,当您要解析 XML 时,请执行以下操作:
DateTime lastUpdated = ... // here set the time of the last update
string xmlContent = fetchRSS("http://111.111.11.11/1.xml", lastUpdated);
if(xmlContent != null){
rssDoc = new XmlDocument();
// Load the XML content into a XmlDocument
rssDoc.LoadXml(xmlContent);
}