读取多个RSS源

问题描述:

我目前正试图建立在Windows Phone有点RSS阅读器应用程序7.读取多个RSS源

到目前为止,我能够在不同的全景图显示单一饲料和它工作正常。

但是在我的MainPage上,我想显示来自多个RSS源的最新消息。

例如,我在不同的页面上有feed1,feed2,我想在我的主页上显示来自feed1和feed2(或更多)的最新消息的标题。

这是我一直在使用我的单身饲料代码

Mainpage_Loaded:

WebClient wc = new WebClient(); 
wc.DownloadStringAsync(new Uri("feed-goes-here")); 
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted); 

然后:

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
     { 

      if (e.Error != null) return; 

      XElement xmlItems = XElement.Parse(e.Result); 

      listBox1.ItemsSource = from x in xmlItems.Descendants("item") 
            where x.Element("enclosure") != null && x.Element("description") != null 
            select new RSSitem 
            { 
             Description = x.Element("description").Value.Substring(0,100)+"...", 
             Title = x.Element("title").Value, 
             ImageSource = x.Element("enclosure").FirstAttribute.Value 
            }; 

     } 

我已经有很多的方法来测试解决我的问题,但仍然找不到答案。

所以我的问题是:我如何显示在列表框上,从同一页上的2个不同的饲料最近的新闻? 谢谢你的帮助和你的时间。

+0

问题在哪里? – onof 2012-02-14 15:25:26

首先,我会建议增加一个日期属性的RSSItem:

public class RSSItem 
{ 
    public DateTime Date { get; set; } 
    public string Description { get; set; } 
    public string Title { get; set; } 
    public string ImageSource { get; set; } 
} 

这样,当你下载你的两个RSS源,你可以编织在一起并按日期排序:

private IEnumerable<RSSItem> Weave(List<RSSItem> feed1, List<RSSItem> feed2) 
{ 
    return feed1.Concat(feed2) 
     .OrderByDescending(i => i.Date) 
     .Take(10); // Grab the ten most recent entries 
} 

用法:

using System.Linq; // Don't forget to add using statement for System.Linq 

public class RssReader 
{ 
    private List<IEnumerable<RSSItem>> _feeds = new List<IEnumerable<RSSItem>>(); 

    public void Download() 
    { 
     WebClient wc = new WebClient(); 
     wc.DownloadStringCompleted += this.FeedDownloaded; 
     wc.DownloadStringAsync(new Uri("feed-goes-here")); 
    } 

    private void FeedDownloaded(object sender, DownloadStringCompletedEventArgs e) 
    { 
     if (e.Error != null) return; 

     var xml = XElement.Parse(e.Result); 
     var feed = from x in xml.Descendants("item") 
        where x.Element("enclosure") != null && x.Element("description") != null 
        select new RSSItem() 
        { 
         Date = DateTime.Parse(x.Element("date").Value), 
         Title = x.Element("title").Value, 
         ImageSource = x.Element("enclosure").FirstAttribute.Value 
        }; 

     _feeds.Add(feed); 

     if (_feeds.Count == 2) 
     { 
      var result = this.Weave(_feeds[0], _feeds[1]); 

      // Assign result to the list box's ItemSource 
      // Or better, use data binding. 
     } 
    } 

    /// <summary> 
    /// Combines the two feeds, sorts them by date, and returns the ten most recent entries. 
    /// </summary> 
    private IEnumerable<RSSItem> Weave(IEnumerable<RSSItem> feed1, IEnumerable<RSSItem> feed2) 
    { 
     return feed1.Concat(feed2) 
      .OrderByDescending(i => i.Date) 
      .Take(10); 
    } 
}