0

I'm trying to write a RSS Reader that reads data from a remote XML file, the problem is that when I run application for the first time it downloads normally but when I update the information on XML file and run application again it does not download the new items. I call it on public Main()

WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri("http://suntvhost.do.am/sunnews.xml"));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 

Handler:

private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //This simple test will return an error if the device is not connected to the internet
            if (e.Error != null)
                return;
            XElement xmlitems = XElement.Parse(e.Result);
            // We need to create a list of the elements
            List<XElement> elements = xmlitems.Descendants("item").ToList();
            // Now we're putting the informations that we got in our ListBox in the XAML code
            // we have to use a foreach statment to be able to read all the elements 
            // Description 1 , Link1 , Title1 are the attributes in the RSSItem class that I've already added
            List<RSSItem> aux = new List<RSSItem>();
            foreach (XElement rssItem in elements)
            {
                RSSItem rss = new RSSItem();
                rss.Description1 = rssItem.Element("description").Value;
                rss.Link1 = rssItem.Element("link").Value;
                rss.Title1 = rssItem.Element("title").Value;
                rss.Date1 = rssItem.Element("pubDate").Value;
                aux.Add(rss);

                TextBlock tbTitle = new TextBlock();
                tbTitle.Text = "\n" + rss.Title1;
                ListBoxRss.Items.Add(tbTitle);
             // and so on for the rest of items...
            }
        }
Zara Gheorghe
  • 488
  • 4
  • 13

4 Answers4

3

If you get the same RSS file then this might be due to client side caching.
Forcing Windows Phone to download the RSS file worked for me by adding this HTTP-Header:

wc.Headers[ HttpRequestHeader.IfModifiedSince ] = "Sat, 29 Oct 1994 19:43:31 GMT";
Volker Voecking
  • 5,203
  • 2
  • 38
  • 35
1

All you have to do is the following:

string myUrl = "http://suntvhost.do.am/sunnews.xml?rand={0}";
...
_webClient.DownloadStringAsync(new Uri(string.Format(myUrl, DateTime.Now.Ticks)));
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
0

I would start by registering the event handler before you actually try to get some results. It might be too late then already to register that event handler:

WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri("http://suntvhost.do.am/sunnews.xml"));

Also, just returning when there is an error doesn't make it easy to diagnose any problems, to do something to write it to a log file, etc.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

The problem is that WebClient is caching the data.

More about that here: C# WebClient disable cache

Community
  • 1
  • 1
klm_
  • 1,199
  • 1
  • 10
  • 26