惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
罗磊的独立博客
F
Fortinet All Blogs
T
Threatpost
Y
Y Combinator Blog
博客园_首页
美团技术团队
Security Latest
Security Latest
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
V
V2EX - 技术
The Cloudflare Blog
L
LINUX DO - 热门话题
博客园 - 司徒正美
Jina AI
Jina AI
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Hacker News
The Hacker News
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Latest news
Latest news
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
雷峰网
雷峰网
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
W
WeLiveSecurity
D
DataBreaches.Net
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
S
Securelist
Help Net Security
Help Net Security

博客园 - Magic.Z

几种可能使 Windows 7 睡眠后自动唤醒的原因 IIS 7.5 下载文件遇到 404.3 试用 Entity Framework Power Tools CTP1 通过 WebBrowser 获取网页截图 WPF 客户端上传文件到 ASP.NET MVC 网站 开启 IE9 导航声音 WPF 实现屏幕文字提示 - Magic.Z - 博客园 AJAX 网页保留浏览器前进后退等功能 ADO.NET Entity Framework Feature 中的 CodeFirst 创建数据库和实体类 赠送 MSDN 订阅激活卡 在 ASP.NET WebForm 中使用 Route 在 ASP.NET MVC 中创建自定义 HtmlHelper 注意防范ASP.NET中可能导致信息泄漏的漏洞 用 Windows Media Center 浏览互联网视频 WebMatrix 简介 打印网页中的 GridView - Magic.Z ASP.NET 网站后台任务 Visual Studio 2010 中的 MSDN 帮助文档 在 ADO.NET Entity Framework 4 中使用枚举
在 ASP.NET MVC Web 应用程序中输出 RSS Feeds
Magic.Z · 2010-08-29 · via 博客园 - Magic.Z

RSS全称Really Simple Syndication。一些更新频率较高的网站可以通过RSS让订阅者快速获取更新信息。RSS文档需遵守XML规范的,其中必需包含标题、链接、描述信息,还可以包含发布时间、最后更新时间等信息。

本文将介绍通过LINQ to XML生成XML文档,并在ASP.NET MVC Web应用程序中输出。

在生成RSS文档前,先简单了解一下RSS的结构。根节点rss下有channel节点,channel节点的一些子节点(title,link,description)包含了该RSS的部分描述信息。channel下可包含多个item节点用来表示多个内容信息,如博客中的文章、论坛中的帖子。

代码

 1 <rss version="2.0">
 2   <channel>
 3     <title>channel标题</title>
 4     <link>网页地址</link>
 5     <description>channel描述</description>
 6     <item>
 7       <title>内容1标题</title>
 8       <description>内容1描述</description>
 9       <link>内容1链接</link>
10     </item>
11     <item>
12       <title>内容2标题</title>
13       <description>内容2描述</description>
14       <link>内容2链接</link>    </item>
15   </channel>
16 </rss>

1. 用LINQ to XML生成类似上述的文档。

1.1 新建一个XDocument,添加根节点和相关属性描述。

代码

1 XDocument doc = new XDocument(
2     new XDeclaration("1.0""utf-8""yes"),    // XML文档声明
3     new XElement("rss",    // 根节点
4     new XAttribute("version""2.0"),    // rss节点的属性
5     new XElement(channel    // rss的子节点channel
6         )));                    )));

1.2 处理channel节点和它的相关描述。

代码

1 XElement channel = new XElement("channel");    // channel节点
2 channel.Add(new XElement[]{
3     new XElement("title","Test"),    // channel标题
4     new XElement("link","http://localhost"),    // 页面链接
5     new XElement("description","Test RSS")    // channel描述
6 });

1.3 往channel节点增加内容信息,rssFeedList是 List<RssFeed>类型的。由于item数量不固定,这里用了foreach将list中的每一个内容信息都加到channel。

代码

 1 foreach (var rssFeed in rssFeedList)    // 对rssFeed集合中的每个元素进行处理
 2 {
 3     XElement item = new XElement("item"new XElement[]{    // 生成一个新的item节点
 4         new XElement("title",rssFeed.Title),    // 为新的item节点添加子节点
 5         new XElement("description",rssFeed.Description),
 6         new XElement("link",rssFeed.Link),
 7         new XElement("pubDate",rssFeed.PublishDate)
 8     });
 9     channel.Add(item);    // 将新的item节点添加到channel中
10 }

2. 创建RssFeedResult类

我们写一个RssFeedResult类,继承自ActionResult,以便在ASP.NET MVC的controller中返回RSS。关于这部分内容可参考之前的一篇文章《让ASP.NET MVC页面返回不同类型的内容》。

代码

 1 public class RssFeedResult : ActionResult
 2 {
 3     List<RssFeed> Data { getset; }
 4 
 5     public RssFeedResult(List<RssFeed> data)
 6     {
 7         Data = data;
 8     }
 9 
10     public override void ExecuteResult(ControllerContext context)
11     {
12         if (context == null)
13         {
14             throw new ArgumentNullException("context");
15         }
16 
17         HttpResponseBase response = context.HttpContext.Response;
18         response.ContentType = "text/xml";    // 设置HTTP头中的ContentType
19         XDocument result= RssFeedHelper.GetRssFeed(Data);    // 获取XML数据
20         response.Write(result.ToString());    // 将XML数据写入response中
21     }
22 }

3. 在controller中使用

我们只要在controller中调用RssFeedResult(rssFeedList)方法即可返回RSS页面了。

代码

public RssFeedResult Rss()
{
    
// 添加2个测试用的数据
    RssFeed r1 = new RssFeed { Description = "Test1", Link = "http://localhost/1", Title = "Test1", PublishDate = DateTime.Now };
    RssFeed r2 
= new RssFeed { Description = "Test2", Link = "http://localhost/2", Title = "Test2", PublishDate = DateTime.Now };
    List
<RssFeed> rssFeedList = new List<RssFeed>();
    rssFeedList.Add(r1);
    rssFeedList.Add(r2);
            
    
// 返回RSS
    return new RssFeedResult(rssFeedList);
}

示例下载 (Visual Studio 2010) 

另外,还有一个工具ASP.NET RSS Toolkit,有需要的可以参考一下。