在CodePlex上面找到这个,下载后,安装,然后在自己的东东里面引用一下.很方便的.
例子是,抓取给予的地址的rss内容,可能是多条,然后全部放一起进行排序后,取出前N条,利用GridView显示出来.
1.Default.aspx页面:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeBehind
=
"
Default.aspx.cs
"
Inherits
=
"
Web2._Default
"
%>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
Untitled Page
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
AutoGenerateColumns
=
"
false
"
>
<
Columns
>
<
asp:HyperLinkField HeaderText
=
"
标题
"
DataTextField
=
"
title
"
DataNavigateUrlFields
=
"
link
"
/>
<
asp:BoundField DataField
=
"
description
"
HeaderText
=
"
概述
"
/>
<
asp:BoundField DataField
=
"
author
"
HeaderText
=
"
作者
"
/>
<
asp:BoundField DataField
=
"
date
"
HeaderText
=
"
日期
"
/>
</
Columns
>
</
asp:GridView
>
</
div
>
</
form
>
</
body
>
</
html
>
2.Default.aspx.cs页面:
using
System;
using
System.Collections;
using
System.Collections.Generic;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
Argotic.Common;
using
Argotic.Syndication;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
Web2
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public partial class _Default : System.Web.UI.Page
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataTable table = new DataTable();
protected void Page_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
table.Columns.Add(new DataColumn("title"));
table.Columns.Add(new DataColumn("description"));
table.Columns.Add(new DataColumn("author"));
table.Columns.Add(new DataColumn("date"));
table.Columns.Add(new DataColumn("link"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int counter = 3; //抓取的条数,此时,最终要显示的条数必须等于或小于每个地址抓取的条数,否则可能不一定是按全部时间排序后的结果.
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加两条地址,抓取Rss记录.
List<Uri> feedUrls = new List<Uri>();
feedUrls.Add(new Uri("http://www.cnblogs.com/davidluozw/CommentsRSS.aspx"));
feedUrls.Add(new Uri("http://feed.yulu.info"));
IEnumerator em = feedUrls.GetEnumerator();
while (em.MoveNext())
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
RssFeed feed = RssFeed.Create((Uri)em.Current);
table.Merge(catchRss(feed, counter));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//对已经按所有地址抓取的所有记录混到一起,然后按时间排序,取前counter条.
DataRow[] rows = table.Select("","date desc");
DataTable tempTable = table.Clone();
tempTable.Rows.Clear();
foreach (DataRow row in rows)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tempTable.ImportRow(row);
if (--counter==0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
table = tempTable;
//将排序后的结果作为数据源绑定给GridView,显示.
GridView1.DataSource = table;
GridView1.DataBind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DataTable catchRss(RssFeed feed,int counter)
{
//收取Rss.
//SyndicationResourceLoadSettings settings = new SyndicationResourceLoadSettings();
//settings.RetrievalLimit = 10;
DataTable rtnTable = table.Clone();
rtnTable.Rows.Clear();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (RssItem item in feed.Channel.Items) //遍历此feed中的每一项.
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
rtnTable.Rows.Add(item.Title,item.Description,item.Author,item.PublicationDate.ToShortDateString(),item.Link.ToString());
if (--counter==0)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return rtnTable;
}
}
}
就这样!!
![](https://www.cnblogs.com/Emoticons/xd/008.gif)