2014-06-24 43 views
0

我想寫一個RSS閱讀器,從遠程XML文件中讀取數據,問題是,當我第一次運行應用程序時,它通常下載,但當我更新XML文件上的信息並再次運行應用程序,它不會下載新項目。 我把它放在public Main()如何強制WebClient DownloadStringAsync每次在應用程序啓動

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

處理程序:

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

回答

3

如果你得到同樣的RSS文件,那麼這可能是由於客戶端緩存。
強制的Windows Phone加入這個HTTP標頭下載RSS文件爲我工作:

wc.Headers[ HttpRequestHeader.IfModifiedSince ] = "Sat, 29 Oct 1994 19:43:31 GMT"; 
+0

感謝,幫助我呢!祝你有美好的一天! – WdarinS

0

我會通過前登錄事件處理你實際上試圖得到一些結果開始。這可能是太晚了,然後就來註冊事件處理程序:

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

此外,剛剛返回時出現錯誤並不可以很容易地診斷問題,做一些事來寫日誌文件等。

1

所有你需要做的是以下幾點:

string myUrl = "http://suntvhost.do.am/sunnews.xml?rand={0}"; 
... 
_webClient.DownloadStringAsync(new Uri(string.Format(myUrl, DateTime.Now.Ticks))); 
相關問題