2

I was just creating a simple internal feedback (RSS) reader (using XML) for intranet; I started by using ListView and populated it with XmlTextReader & XmlDocument.

But this reader is not useful unless I introduce a feature for auto refreshing my reader as soon as the source xml file is updated. One way I could think is:

Fetch the complete file, then compare number of prev/new ChildNodes; if new > prev then load/refresh the Winform. But hundreds of client forms sending these un-important requests will make a joke of the network server's response-time!

I feel I should use some logic to compare Date & Time of creation/update of the XML file. But I've never used such function till today... what do you say, any better idea?

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
gsvirdi
  • 406
  • 2
  • 7
  • 22
  • help required regarding the DateTime function, & how to compare them if the file on intranet url is updated. – gsvirdi May 17 '12 at 07:42
  • How you fetch XML currently? WCF,http,...? – Yasser Zamani May 17 '12 at 07:44
  • I started by using ListView (in my Windows Forms) and populated it with XmlTextReader & XmlDocument with C Sharp `rssReader = new XmlTextReader(RssFile.ToString()); rssDoc = new XmlDocument(); // Load the XML content into a XmlDocument rssDoc.Load(rssReader);` and RssFile is fetched from `http://111.111.11.11/1.xml` location – gsvirdi May 17 '12 at 07:52
  • 2
    check this question, there are some useful ideas to improve your polling algorithm http://stackoverflow.com/questions/939642/policy-for-polling-rss – Ventsyslav Raikov May 17 '12 at 07:52
  • @Bond Thx a lot for the link, but unfortunately I'm just a beginner and this is a kind of 1st project to understand the network... which means I'm not well educated to understand these advance points. Maybe I will need to get some code to understand what they mean. Btw I'm using windows forms and so I'm not able to understand if I can do anything about using http cache/headers etc in my project. I'm just trying to read a xml file as/when it is updated on the serverr. In the meanwhile I'm consulting google regarding those pooling algo things to know more about them... Thx again :) – gsvirdi May 17 '12 at 08:04

1 Answers1

0

You can fetch this file like this:

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;
}

then when you want to parse the XML do the following:

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);
}
Muaz Othman
  • 427
  • 4
  • 13