.Net3.5創建RSS
2010-08-10 10:27 空逸云 閱讀(302) 評論(0) 收藏 舉報在之前,創建RSS必須要手動調用XML API生成XML,LINQ to XML的出現大大減少了工作量(可參考LINQ構建RSS).然而.在.Net3.5中.微軟提供了更方便快捷的API.詳見System.ServiceModel.Syndicatio命名空間但是.我們使用到的只有 SyndicationFeed 類,SyndicationItem 類 ,Rss20FeedFormatter 類. RSS的標準協議還是比較雜的.對于這方面不甚了解的同學.可以參考關于 Blog 和 RSS 的全面介紹 .
首先.我們看一段RSS示例
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
- <channel>
<title>博客園-空逸云</title>
<link>http://www.rzrgm.cn/kongyiyun/</link>
<description>路漫漫其修遠兮,吾將上下而求索.</description>
<language>zh-cn</language>
<lastBuildDate>Tue, 10 Aug 2010 02:00:54 GMT</lastBuildDate>
<pubDate>Tue, 10 Aug 2010 02:00:54 GMT</pubDate>
<ttl>60</ttl>
- <item>
<title>星級評分公式</title>
<link>http://www.rzrgm.cn/kongyiyun/archive/2010/08/06/1794441.html</link>
<dc:creator>空逸云</dc:creator>
<author>空逸云</author>
<pubDate>Fri, 06 Aug 2010 13:31:00 GMT</pubDate>
<guid>http://www.rzrgm.cn/kongyiyun/archive/2010/08/06/1794441.html</guid>
- <description>
- <![CDATA[ <p>作者: <a href="http://www.rzrgm.cn/kongyiyun/" target="_blank">空逸云</a> 發表于 2010-08-06 21:31 <a href="http://www.rzrgm.cn/kongyiyun/archive/2010/08/06/1794441.html" target="_blank">原文鏈接</a> 閱讀: 6 評論: 0</p>.....
]]>
</description>
</item>
</channel>
</rss>
再比較MSDN.不難發現.必要有一個channel節.其中包含有title,link,description等屬性.再下.每個item就是我們所要呈現的重要內容.在這里.重點就是SyndicationFeed 類和SyndicationItem類.我的實現如下.
首先建立了一個RssBuilder類.用于創建必要的SyndicationFeed.
/// <summary>
/// </summary>
public class RssBuilder
{
private int itemAmount = ACookContext.Current.ACookSettingsConfig.RssAmount;
private int _userId = 0;
public RssBuilder() { }
public RssBuilder(int userId) { _userId = userId; }
public SyndicationFeed BuildRss()
{
var recipes = GetSources();
var ctx = ACookContext.Current;
string feedTitle = string.Format("{0} {1}'s Rss", ctx.ACookSettingsConfig.SiteName, _userId == 0 ? string.Empty : ctx.ACookAppConfig.Author);
string targetUrl = string.Format("{0}/{1}", ctx.ACookSettingsConfig.DomainName, ctx.ACookAppConfig.Application);
SyndicationFeed feed = new SyndicationFeed(feedTitle, "ACook's Rss", new Uri(targetUrl), "What's this?", DateTime.Now)
{
Language = "zh-cn"
};
List<SyndicationItem> items = new List<SyndicationItem>();
foreach (var x in recipes)
{
SyndicationItem item = new SyndicationItem(x.RecipeTitle, x.CookSteps, new Uri(string.Format("http://www.acook.cc/recipe/{0}.aspx", x.RecipeId)), x.RecipeSample, x.PTime);
item.Categories.Add(new SyndicationCategory(x.Category.CategoryTitle));
item.Authors.Add(new SyndicationPerson(x.Publisher.Author));
items.Add(item);
}
feed.Items = items;
return feed;
}
private string BuildContent(ACookRecipe recipe)
{
//TODO:生成頁面的呈現內容
return recipe.CookSteps;
}
private IEnumerable<ACookRecipe> GetSources()
{
//TODO:獲取數據源,
}
}
然后.再創建一個Rss.cs IHttpHandler類.此處要引用System.ServiceModel.Web程序集,用戶呈現RSS內容實現如下.
public void ProcessRequest(HttpContext context)
{
RssBuilder builder = new RssBuilder(ACookContext.Current.ACookAppConfig.UserId);
var feed = builder.BuildRss();
context.Response.ContentType = "text/xml";
Rss20FeedFormatter rssfm = new Rss20FeedFormatter(feed);
XmlWriter rssWriter = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
rssfm.WriteTo(rssWriter);
rssWriter.Close();
}
由于是要呈現于頁面當中.我們就是用了Response.OutputStream,如果是寫入XML文件中.可以可以XmlWriter.Create("rss.xml");
最后.再在WebConfig的HttpHandlers節中加入
add verb="*" path="rss.rss" type="ACook.FrameWork.Handler.Rss,ACook.FrameWork"/>
即可.
好了.現在我們來訪問我們創建的RSS吧.

出處:http://kongyiyun.cnblogs.com
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
浙公網安備 33010602011771號