【问题标题】:How to order an RSS Feed from a News Site by the Publication Date如何在发布日期之前从新闻站点订购 RSS 提要
【发布时间】:2016-03-24 09:29:38
【问题描述】:

我正在使用 .net 4 并从新闻网站阅读 RSS 提要 - http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk

我已经使用转发器将提要进入页面,但我刚刚注意到提要并没有一直对发布日期 DESC 进行排序。似乎有一个奇怪的分组。

如何明确指定它是按出版日期 DESC 排序的?

这就是我目前所拥有的......

private void PopulateRssFeed()
{
    //BBC UK 
    string RssFeedUrl = "http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk";

    List<Feeds> feeds = new List<Feeds>();
    try
    {
        XDocument xDoc = new XDocument();
        xDoc = XDocument.Load(RssFeedUrl);

        //Take 3 Limits the number of items to display
        //i.e -
        //var items = (from x in xDoc.Descendants("item").Take(3)
        var items = (from x in xDoc.Descendants("item")
                     select new
                     {
                         title = x.Element("title").Value,
                         link = x.Element("link").Value,
                         pubDate = x.Element("pubDate").Value,
                         description = x.Element("description").Value
                     });


        if (items != null)
        {
            foreach (var i in items)
            {

                Feeds f = new Feeds
                {
                    Title = i.title,
                    Link = i.link,
                    PublishDate = i.pubDate,
                    Description = i.description
                };

                feeds.Add(f);

            }

        }

        Repeater1.DataSource = feeds;
        Repeater1.DataBind();

    }
    catch (Exception ex)
    {
        throw ex;
    }
}

【问题讨论】:

    标签: c# .net xml linq rss


    【解决方案1】:

    使用 LINQ orderby ... descending

    var items = (from x in xDoc.Descendants("item")
                 orderby (DateTime)x.Element("pubDate") descending
                 select new
                 {
                     title = x.Element("title").Value,
                     link = x.Element("link").Value,
                     pubDate = x.Element("pubDate").Value,
                     description = x.Element("description").Value
                 });
    

    顺便说一句,items 永远不可能是null,所以不需要if (items != null) 检查。

    【讨论】:

    • 谢谢你 - 也谢谢你的物品空提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多