在 ASP.NET MVC Web 应用程序中输出 RSS Feeds
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 { get; set; }
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 MVC Web 应用程序中输出 RSS Feeds的更多相关文章
- ASP.NET MVC3 Web应用程序中启用GZip压缩示例
http://www.mzwu.com/article.asp?id=3284 自定义一个筛选器,继承于GZipAttribute: using System;using System.IO.Comp ...
- ASP.NET MVC & Web API项目中集成MEF
1.实现方式 //WebApi需要实现的接口 System.Web.Http.Dependencies.IDependencyResolver //MVC需要实现的接口 System.Web.Mvc. ...
- ASP.NET MVC - 探究应用程序文件夹
为了学习 ASP.NET MVC,我们将构建一个 Internet 应用程序. 第 2 部分:探究应用程序文件夹. MVC 文件夹 一个典型的 ASP.NET MVC Web 应用程序的文件夹内容如下 ...
- ABP 教程文档 1-1 手把手引进门之 AngularJs, ASP.NET MVC, Web API 和 EntityFramework(官方教程翻译版 版本3.2.5)含学习资料
本文是ABP官方文档翻译版,翻译基于 3.2.5 版本 转载请注明出处:http://www.cnblogs.com/yabu007/ 谢谢 官方文档分四部分 一. 教程文档 二.ABP 框架 三. ...
- ASP.NET Core Web 应用程序系列(三)- 在ASP.NET Core中使用Autofac替换自带DI进行构造函数和属性的批量依赖注入(MVC当中应用)
在上一章中主要和大家分享了在ASP.NET Core中如何使用Autofac替换自带DI进行构造函数的批量依赖注入,本章将和大家继续分享如何使之能够同时支持属性的批量依赖注入. 约定: 1.仓储层接口 ...
- ASP.NET Core Web 应用程序系列(二)- 在ASP.NET Core中使用Autofac替换自带DI进行批量依赖注入(MVC当中应用)
在上一章中主要和大家分享在MVC当中如何使用ASP.NET Core内置的DI进行批量依赖注入,本章将继续和大家分享在ASP.NET Core中如何使用Autofac替换自带DI进行批量依赖注入. P ...
- [译]ABP框架使用AngularJs,ASP.NET MVC,Web API和EntityFramework构建N层架构的SPA应用程序
本文转自:http://www.skcode.cn/archives/281 本文演示ABP框架如何使用AngularJs,ASP.NET MVC,Web API 和EntityFramework构建 ...
- 【转载】ASP.NET MVC Web API 学习笔记---第一个Web API程序
1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序
http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...
随机推荐
- mysql中的sql时间格式转换
from_unixtime(unix_timestamp, format) 把时间戳转化为指定的格式 as: select from_unixtime(addTime, '%Y-%m-%d %h:%i ...
- win7 蛋疼的时间格式转化
win7系统 获得系统时间为 2015年1月1日 星期5 10:10 数据库中时间格式 是不认识的 转化为 DateTime.Now.ToString("yyyy-MM-dd HH:mm:s ...
- Asp.net 导入Excel数据
前台代码: <body> <form id="form1" runat="server"> <div> <asp:Fi ...
- python with关键字学习
1.with语句时用于对try except finally 的优化,让代码更加美观, 例如常用的开发文件的操作,用try except finally 实现: f=open('file_name', ...
- Python中使用ElementTree解析xml
在Python中,ElementTree是我们常用的一个解析XML的模块 1.导入ElementTree模块 from xml.etree import ElementTree as ET 2.初始化 ...
- Django数据库配置
将Django使用数据库由默认的sqlite3更改为mysql: 1.安装mysql驱动程序 MySQLdb(mysql-python) mysqlclient Connector/Python Py ...
- js中的字典
最近项目JS中需要建一个特殊的颜色库,需要用到类似C#中的dictionary的概念 然后一查发现JS没有dictionary 而是Array 初始化Array colorDic = new Arra ...
- spring mvc处理流程概述
大部分Java应用都是Web应用,展现层是Web应用不可忽略的重要环节.Spring为展现层提供了一个优秀的Web框架-Spring MVC.和众多其他Web框架一样,它基于MVC设计理念,此外,它采 ...
- Memcached(六)Memcached的并发实例
package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAddress; i ...
- iOS 页面间传值 之 单例传值 , block 传值
ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...