<%@ OutputCache Duration="60" VaryByParam="Type" %>
<%-- 因为不是网页,所以删除@ Page之后的HTML.
OutputCache表示当生成一次XML输出,在接下来的60分钟,其他请求XML的输出将存储于缓存中, –%>
然后,Feed.aspx Page_Load事件处理程序中的代码如下,数据库使用pubs 数据库,并使用LINQ to SQL作数据访问层:
protected void Page_Load(object sender, EventArgs e) { // 获得图书表中的数据并pubdate降序排列 PubsDataContext db = new PubsDataContext(); var dataItems = from book in db.Titles orderby book.pubdate descending select book; // 确定显示在阅读器中最大列表数 const int maxItemsInFeed = 10; // 确定是否输出一个Atom或RSS格式 bool outputRss = (Request.QueryString["Type"] == "RSS"); bool outputAtom = !outputRss; // 输出适当的ContentType if (outputRss) Response.ContentType = "application/rss+xml"; else if (outputAtom) Response.ContentType = "application/atom+xml"; // 创建RSS格式具体的属性 SyndicationFeed myFeed = new SyndicationFeed(); myFeed.Title = TextSyndicationContent.CreatePlaintextContent( "chinabooks.com图书网" ); myFeed.Description = TextSyndicationContent.CreatePlaintextContent( "chinabooks.com图书网最新的图书." ); myFeed.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(GetFullyQualifiedUrl("~/Default.aspx")))); myFeed.Links.Add(SyndicationLink.CreateSelfLink(new Uri(GetFullyQualifiedUrl(Request.RawUrl)))); myFeed.Copyright = TextSyndicationContent.CreatePlaintextContent( "版权所有chinabooks.com书店" ); myFeed.Language = "en-us"; // 创建并填充SyndicationItems List<SyndicationItem> feedItems = new List<SyndicationItem>(); foreach (Title t in dataItems.Take(maxItemsInFeed)) { // Atom items 必须要有author, 因此没有作者的这一项就跳到下一项 if (outputAtom && t.TitleAuthors.Count == 0) continue; SyndicationItem item = new SyndicationItem(); item.Title = TextSyndicationContent.CreatePlaintextContent(t.title1); item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(GetFullyQualifiedUrl("~/Titles.aspx")))); item.Summary = TextSyndicationContent.CreatePlaintextContent(t.notes); item.Categories.Add(new SyndicationCategory(t.type)); item.PublishDate = t.pubdate; foreach (TitleAuthor ta in t.TitleAuthors) { SyndicationPerson authInfo = new SyndicationPerson(); authInfo.Email = ta.Author.au_lname + "@example.com"; authInfo.Name = ta.Author.au_lname+ta.Author.au_fname; item.Authors.Add(authInfo); // RSS源只能有一个作者,所以退出循环。第一个作者已经添加 if (outputRss) break; } // 添加到feedItems feedItems.Add(item); } myFeed.Items = feedItems; // 用response返回XML数据源 XmlWriterSettings outputSettings = new XmlWriterSettings(); outputSettings.Indent = true; //(使用注释便于阅读) XmlWriter feedWriter = XmlWriter.Create(Response.OutputStream, outputSettings); if (outputAtom) { // 使用 Atom 1.0 格式标准 Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(myFeed); atomFormatter.WriteTo(feedWriter); } else if (outputRss) { // 否则使用 RSS 2.0 格式 Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(myFeed); rssFormatter.WriteTo(feedWriter); } feedWriter.Close(); }
结果
从网站的一个页面上显示另一个网站的Syndication RSS Feed
除了创建一个SyndicationFeed外,还可以使用SyndicationFeed的Load方法获取另一个网站的RSS,比如博客园:http://www.cnblogs.com/all(具体代码请下载最后给出的项目):
接下来
记得在网站首页的头部新增meta链接到输出feed的HTML页面。使得小RSS图标发光的显示在Web浏览器地址栏中。
<link rel="Alternate" type="application/atom+xml" href="Feed.aspx?Type=Atom" /> <link rel="Alternate" type="application/rss+xml" href="Feed.aspx?Type=RSS" />

最后