C# 如何在发布日期前从新闻网站订购RSS源
我正在使用.net 4并阅读新闻网站上的RSS源- 我使用转发器将feed放入页面,但我刚刚注意到feed并不总是对发布日期进行排序。似乎有一个奇怪的群体 如何明确指定它是按发布日期DESC排序的 这就是我目前所拥有的C# 如何在发布日期前从新闻网站订购RSS源,c#,.net,xml,linq,rss,C#,.net,Xml,Linq,Rss,我正在使用.net 4并阅读新闻网站上的RSS源- 我使用转发器将feed放入页面,但我刚刚注意到feed并不总是对发布日期进行排序。似乎有一个奇怪的群体 如何明确指定它是按发布日期DESC排序的 这就是我目前所拥有的 private void PopulateRssFeed() { //BBC UK string RssFeedUrl = "http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk"; List<Fe
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;
}
}
private void PopulateRssFeed()
{
//英国广播公司
字符串RssFeedUrl=”http://feeds.bbci.co.uk/news/uk/rss.xml?edition=uk";
列表提要=新列表();
尝试
{
XDocument xDoc=新的XDocument();
xDoc=XDocument.Load(RssFeedUrl);
//Take 3限制要显示的项目数
//i、 e-
//var items=(从xDoc.subjects(“item”)中的x开始)。取(3)
var items=(从xDoc.subjects(“item”)中的x开始)
选择新的
{
title=x.元素(“title”).值,
link=x.元素(“link”).值,
pubDate=x.Element(“pubDate”).值,
描述=x.元素(“描述”).值
});
如果(项!=null)
{
foreach(项目中的var i)
{
feed f=新feed
{
Title=i.Title,
Link=i.Link,
PublishDate=i.PublishDate,
描述
};
添加(f);
}
}
Repeater1.DataSource=feed;
Repeater1.DataBind();
}
捕获(例外情况除外)
{
掷骰子;
}
}
使用LINQ:
另外,项
永远不能为空
,因此如果(项!=null)
不需要检查。使用LINQ:
顺便说一句,
项
永远不能为空
因此如果(项!=null)
检查是不需要的。谢谢-也谢谢你的项空提示。谢谢-也谢谢你的项空提示。
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
});