[英]How to order an RSS Feed from a News Site by the Publication Date
I am using .net 4 and reading a RSS feed from a News website - http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk 我正在使用.net 4并从新闻网站上阅读RSS源 - http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk
I have got the feed coming into the page using a repeater but I have just noticed that the feeds are not sorting Publication Date DESC all the time. 我已经使用转发器将订阅源进入页面,但我注意到订阅源并未始终对发布日期DESC进行排序。 There seems to be a strange grouping. 似乎有一个奇怪的分组。
How am I able to explicitly specify that it is sorted by Publication Date DESC? 我如何能够明确指定它按发布日期DESC排序?
This is what I have so far... 这就是我到目前为止......
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;
}
}
Use LINQ orderby ... descending
: 使用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
});
As an aside, items
can never be null
so if (items != null)
check is not needed. 另外, items
永远不能为null
因此if (items != null)
不需要检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.