阅读多个RSS源

时间:2012-02-14 11:14:31

标签: c# silverlight linq windows-phone-7

我目前正在尝试在Windows Phone 7上创建一个小的RSS阅读器应用程序。

到目前为止,我能够在不同的全景图上显示单个Feed并且工作正常。

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

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

以下是我一直用于单个Feed的代码

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个不同Feed的最新消息? 感谢您的帮助和时间。

1 个答案:

答案 0 :(得分:0)

首先,我建议将一个Date属性添加到R​​SSItem:

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