如何在发布日期之前从新闻站点订购RSS源

时间:2016-03-24 09:29:38

标签: c# .net xml linq rss

我正在使用.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;
    }
}

1 个答案:

答案 0 :(得分:0)

使用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)检查。