阅读多个 RSS 源

发布于 2025-01-06 04:15:00 字数 1399 浏览 1 评论 0原文

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

到目前为止,我能够在不同的全景图上显示单个提要,并且运行良好。

然而,在我的主页上,我想显示来自多个 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 个不同源的最新新闻? 感谢您的帮助和您的时间。

I'm currently trying to create a little RSS reader application on Windows Phone 7.

So far I'm able to display single feeds on different Panoramas and it works fine.

However on my MainPage I want to display the most recent news coming from multiple RSS feeds.

For example I have feed1, feed2 on different pages, and I'd like to show the title of the most recent news coming from both feed1 and feed2 (or more) on my homepage.

Here is the code I've been using for my single feeds

Mainpage_Loaded:

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

And then :

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

        }

I've been testing a lot of methods to solve my problem but still couldn't find the answer.

So my question is : How do i display on a listbox, the most recent news from 2 different feeds on the same page ?
Thank you for your help and for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2025-01-13 04:15:00

首先,我建议向 RSSItem 添加 Date 属性:

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

First, I would recommend adding a Date property to RSSItem:

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

This way, when you download your two RSS feeds, you can weave them together and sort by date:

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
}

Usage:

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文