首页 > 代码库 > 使用 SyndicationFeed 输出 Rss

使用 SyndicationFeed 输出 Rss

以前生成 RSS 都是使用拼接 Xml 的方式生成的,不仅麻烦而且还不规范。

 1         #region 输出指定分类编号的消息源内容... 2  3         /// <summary> 4         /// 输出指定分类编号的消息源内容。 5         /// </summary> 6         public void OutputFeed() 7         { 8             //int categoryId, string customUrl 9             int categoryId = 0;10             string customUrl = string.Empty;11             if (!string.IsNullOrEmpty(RequestUtility.GetQueryString("CategoryId")))12             {13                 categoryId = Convert.ToInt32(RequestUtility.GetQueryString("CategoryId"));14             }15             if (!string.IsNullOrEmpty(RequestUtility.GetQueryString("Custom")))16             {17                 customUrl = RequestUtility.GetQueryString("Custom");18             }19             StringBuilder xml = new StringBuilder();20             xml.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");21             xml.Append("<rss version=\"2.0\">\n");22             xml.Append("<channel>\n");23             CategoryInfo category = new CategoryInfo();24             if (categoryId == 0 && string.IsNullOrEmpty(customUrl))25             {26                 xml.AppendFormat("<title>{0}</title>\n", string.Format(MemoryCacheProvider.GetLanguage("WebRssTitle", WeilogContext.Current.Application.Prefix), SettingInfo.Name));27             }28             else if (categoryId == 0 && !string.IsNullOrEmpty(customUrl))29             {30                 category = CategoryService.GetCategory(Provider, customUrl);31                 xml.AppendFormat("<title>{0}</title>\n", string.Format(MemoryCacheProvider.GetLanguage("WebCategoryRssTitle", WeilogContext.Current.Application.Prefix), category.Name, SettingInfo.Name));32             }33             else34             {35                 category = CategoryService.GetCategory(Provider, categoryId);36                 xml.AppendFormat("<title>{0}</title>\n", string.Format(MemoryCacheProvider.GetLanguage("WebCategoryRssTitle", WeilogContext.Current.Application.Prefix), category.Name, SettingInfo.Name));37             }38             xml.AppendFormat("<link>{0}</link>\n", SettingInfo.Url);39             xml.AppendFormat("<description>{0}</description>\n", SettingInfo.Description);40             xml.AppendFormat("<language>{0}</language>\n", SettingInfo.Language);//    <language>zh-cn</language>41             xml.AppendFormat("<copyright>{0}</copyright>\n", "Copyright " + SettingInfo.Name);42             xml.AppendFormat("<webMaster>{0}</webMaster>\n", SettingInfo.SmtpMail);43             xml.AppendFormat("<generator>{0}</generator>\n", WeilogContext.Current.Application.FullVersion);44             xml.Append("<image>\n");45             xml.AppendFormat("\t<title>{0}</title>\n", SettingInfo.Name);46             xml.AppendFormat("\t<url>{0}</url>\n", "/Common/Images/Logo.jpg");47             xml.AppendFormat("\t<link>{0}</link>\n", SettingInfo.Url);48             xml.AppendFormat("\t<description>{0}</description>\n", SettingInfo.Description);49             xml.Append("</image>\n");50             int totalRecords = 0;51             List<PostInfo> articleList = new List<PostInfo>();52             articleList = PostService.GetPostList(base.Provider, categoryId, null, null, PostType.Post, null, null, null, OrderField.ByPublishTime, OrderType.Desc, 1, 20, out totalRecords);53             foreach (PostInfo item in articleList)54             {55                 xml.Append("<item>\n");56                 xml.AppendFormat("\t<link>{0}</link>\n", string.Format(SitePath.PostLinkFormat, SettingInfo.Url, item.Locator));57                 xml.AppendFormat("\t<title>{0}</title>\n", item.Title);58                 xml.AppendFormat("\t<author>{0}</author>\n", item.AuthorName);59                 xml.AppendFormat("\t<category>{0}</category>\n", CategoryService.GetCategory(Provider, item.CategoryId).Name);60                 xml.AppendFormat("\t<pubDate>{0}</pubDate>\n", item.PublishTime);61                 //xml.AppendFormat("\t<guid>{0}</guid>\n", string.Format(WebPath.PostLinkFormat, SettingInfo.Url, item.CustomUrl));62                 xml.AppendFormat("\t<description>{0}</description>\n", StringUtil.CDATA(string.IsNullOrEmpty(item.Password) ? (SettingInfo.RssType == 0 ? item.Excerpt : item.Content) : MemoryCacheProvider.GetLanguage("MsgEncContent", WeilogContext.Current.Application.Prefix)));63                 xml.Append("</item>\n");64             }65             xml.Append("</channel>\n");66             xml.Append("</rss>");67             HttpContext.Current.Response.ContentType = "text/xml";68             HttpContext.Current.Response.Write(xml);69         }70 71         #endregion

前段时间看老外的项目里用到了 SyndicationFeed 这个类来生成 Rss,索性自己做项目的时候也用了一下,果然事半功倍,只需要简洁的代码便可输出 Rss。我的项目是 MVC 的。

 1         /// <summary> 2         /// 文章订阅。 3         /// </summary> 4         /// <returns>视图的执行结果。</returns> 5         public ActionResult PostFeed() 6         { 7             var feed = new SyndicationFeed( 8                                     base.Settings["Name"].ToString(), 9                                     base.Settings["Description"].ToString(),10                                     new Uri(Settings["Url"].ToString()),11                                     "BlogRSS",12                                     DateTime.UtcNow);13 14             if (!(bool)Settings["Status"])15                 return new FeedActionResult() { Feed = feed };16 17             var items = new List<SyndicationItem>();18             var posts = PostService.GetPostList(Provider, Data.Common.OrderField.ByPublishTime, Data.Common.OrderType.Desc, 20);19             foreach (var post in posts)20             {21                 string blogPostUrl = Url.RouteUrl("Post", new { Id = post.Id }, "http");22                 items.Add(new SyndicationItem(post.Title, post.Content, new Uri(blogPostUrl), String.Format("Blog:{0}", post.Id), post.PublishTime));23             }24             feed.Items = items;25             return new FeedActionResult() { Feed = feed };26         }

最后上一张效果图: