通过本文你将可以知道如下内容:
1.什么是RSS
2.怎样给自己的网站创建一个RSS源
3.如何订阅人家网站上的RSS
什么是RSS?简单来说,RSS就是一种简单的信息发布和传递方式,使得一个网站可以方便地调用其他提供RSS订阅服务的网站的内容,从而形成“新闻聚合”,让网站发布的内容在更大的范围内传播。(引用网上的解释,我觉得这个说的很明确)
RSS源至少应该包含一个频道(channel)
每个
channel里面应该包含标题(title),链接(link),简介(description),项目等
而每一个项目一般都含有一个标题,一段简单的介绍,还有一个URL链接(比如是一个网页的地址)。其他的信息,例如日期,创建者的名字等等,都是可以选择的。(更多关于RSS的标准请自己查阅相关文档)
怎样给自己的网站创建一个RSS源
我们新建一个一般应用文件(CreatRss.ashx).
代码v如下。代码内容很简单,注释也很清楚
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
using#region using
using System;
using System.Data;
using System.Text;
using System.Web;
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Rss
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public class CreatRss : IHttpHandler
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void ProcessRequest(HttpContext context)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
context.Response.ContentType = "text/xml";//设置输出内容为XML格式
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
预备数据#region 预备数据
DataTable dt = new DataTable("RssData");
dt.Columns.Add("title");
dt.Columns.Add("link");
dt.Columns.Add("description");
dt.Columns.Add("author");
dt.Columns.Add("pubDate", typeof(DateTime));
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
dt.Rows.Add(new object[]
{ "IHttpHandlerFactory的运用(权限控制)1", "http://www.cnblogs.com/leleroyn/archive/2008/06/11/1217432.html", "看了Unity&WebForm(1): 自定义IHttpHandlerFactory使用Unity对ASP.NET Webform页面进行依赖注入,我也按奈不住,把上次几个项目里的自定义IHttpHandlerFactory实现权限控制的方案写了下来。。写的匆忙,而且自已水平又有限,望大家指点~0~", "Ants", DateTime.Parse("2008-06-11 15:52") });
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
dt.Rows.Add(new object[]
{ "IHttpHandlerFactory的运用(权限控制)2", "http://www.cnblogs.com/leleroyn/archive/2008/06/11/1217432.html", "看了Unity&WebForm(1): 自定义IHttpHandlerFactory使用Unity对ASP.NET Webform页面进行依赖注入,我也按奈不住,把上次几个项目里的自定义IHttpHandlerFactory实现权限控制的方案写了下来。。写的匆忙,而且自已水平又有限,望大家指点~0~", "Ants", DateTime.Parse("2008-06-12 15:52") });
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
dt.Rows.Add(new object[]
{ "IHttpHandlerFactory的运用(权限控制)3", "http://www.cnblogs.com/leleroyn/archive/2008/06/11/1217432.html", "看了Unity&WebForm(1): 自定义IHttpHandlerFactory使用Unity对ASP.NET Webform页面进行依赖注入,我也按奈不住,把上次几个项目里的自定义IHttpHandlerFactory实现权限控制的方案写了下来。。写的匆忙,而且自已水平又有限,望大家指点~0~", "Ants", DateTime.Parse("2008-06-13 15:52") });
DataView dv = dt.DefaultView;
dv.Sort = "pubDate desc";
#endregion
StringBuilder str = new StringBuilder();
str.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?> ");
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
频道信息区#region 频道信息区
str.Append("<rss version=\"2.0\">"); //起名RSS的版本
str.Append("<channel>");//频道
str.Append("<title>Ants 的博客</title>");//频道的标题
str.Append("<link>http://www.cnblogs.com/leleroyn/</link>");
str.Append("<description>我的宣言:一起学习,大家向上</description>");
str.Append("<author>Ants</author>");
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
内容列表区#region 内容列表区
for (int i = 0; i <= 2; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
str.Append("<item>");
str.Append(string.Format("<title>{0}</title>", context.Server.HtmlEncode(dv[i]["title"].ToString())));//编码的标题
str.Append(string.Format("<link>{0}</link>", dv[i]["link"].ToString()));
str.Append(string.Format("<description>{0}</description>", context.Server.HtmlEncode(dv[i]["description"].ToString())));//编码项目的介绍
str.Append(string.Format("<author>{0}</author>", dv[i]["author"].ToString()));
str.Append(string.Format("<pubDate>{0}</pubDate>", dv[i]["pubDate"].ToString()));
str.Append("</item>");
}
#endregion
str.Append("</channel>");
str.Append("</rss>");
context.Response.Write(str.ToString());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
直接运行这个文件就生成了属于我们的RSS源啦,大家可以试试,效果如下:
怎样在自己的网页中订阅RSS
我就以博客园首页原创区的RSS源为例。
新建一个webform页面
内容如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Rss._Default" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate><ol></HeaderTemplate>
<ItemTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<li>标题:<a href='<%# Eval("link") %>'><%
# Eval("title") %></a>
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
摘要:<%
# Eval("description")%>
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
作者:<%
# Eval("author")%>
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
时间:<%
# Eval("pubDate")%>
<br />
<hr />
</ItemTemplate>
<FooterTemplate> </ol></FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
后台代码如下:
using System;
using System.Linq;
using System.Xml.Linq;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Rss
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class _Default : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
XDocument doc = XDocument.Load("http://www.cnblogs.com/rss");//也可是别的RSS源
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var query = from p in doc.Descendants("item")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
select new
{
title = p.Element("title").Value,
link = p.Element("link").Value,
description = p.Element("description").Value ,
pubDate=p.Element("pubDate").Value,
author = p.Element("author").Value
};//取item下的元素值
Repeater1.DataSource = query;
Repeater1.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行就可以啊。这个就不贴图啦。
本章内容简单但很实用,希望对大家有帮助,献丑啦