一、Rss概述
RSS也叫聚合RSS是在线共享内容的一种简易方式(也叫聚合内容,Really Simple Syndication)。通常在时效性比较强的内容上使用RSS订阅能更快速获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新。
二、Rss格式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<!--
以下是RSS
2
.0的代码样例
-->
< rss version = " 2.0 " >
< channel >
<!-- 总体信息 -->
<!-- 标题 -->
< title > Lift Off News </ title >
<!-- 链接的总地址 -->
< link > http: // liftoff.msfc.nasa.gov/</link>
<!-- 描述文字 -->
< description > Liftoff to Space Exploration. </ description >
<!-- 授权信息 -->
< copyright > Copyright (C) DotNetBips.com. All rights reserved. </ copyright >
<!-- 使用的语言(zh - cn表示简体中文) -->
< language > en - us </ language >
<!-- 发布的时间 -->
< pubDate > Tue, 10 Jun 2003 04 : 00 : 00 GMT </ pubDate >
<!-- 最后更新的时间 -->
< lastBuildDate > Tue, 10 Jun 2003 09 : 41 : 01 GMT </ lastBuildDate >
< docs > http: // blogs.law.harvard.edu/tech/rss</docs>
<!-- 生成器 -->
< generator > Weblog Editor 2.0 </ generator >
<!-- 分享的条目 -->
<!-- 每条RSS信息都包含在item节点中 -->
< item >
<!-- 标题 -->
< title > Star City </ title >
<!-- 链接地址 -->
< link > http: // xxx.aspx</link>
<!-- 内容简要描述 -->
< description >
How do Americans get ready to work with Russians aboard the
International Space Station ? They take a crash course in culture, language
and protocol at Russia ' s Star City.
</ description >
<!-- 发布时间 -->
< pubDate > Tue, 03 Jun 2003 09 : 39 : 21 GMT </ pubDate >
<!-- 目录 -->
< category > IT </ category >
<!-- 作者 -->
< author > bill </ author >
</ item >
</ channel >
</ rss >
< rss version = " 2.0 " >
< channel >
<!-- 总体信息 -->
<!-- 标题 -->
< title > Lift Off News </ title >
<!-- 链接的总地址 -->
< link > http: // liftoff.msfc.nasa.gov/</link>
<!-- 描述文字 -->
< description > Liftoff to Space Exploration. </ description >
<!-- 授权信息 -->
< copyright > Copyright (C) DotNetBips.com. All rights reserved. </ copyright >
<!-- 使用的语言(zh - cn表示简体中文) -->
< language > en - us </ language >
<!-- 发布的时间 -->
< pubDate > Tue, 10 Jun 2003 04 : 00 : 00 GMT </ pubDate >
<!-- 最后更新的时间 -->
< lastBuildDate > Tue, 10 Jun 2003 09 : 41 : 01 GMT </ lastBuildDate >
< docs > http: // blogs.law.harvard.edu/tech/rss</docs>
<!-- 生成器 -->
< generator > Weblog Editor 2.0 </ generator >
<!-- 分享的条目 -->
<!-- 每条RSS信息都包含在item节点中 -->
< item >
<!-- 标题 -->
< title > Star City </ title >
<!-- 链接地址 -->
< link > http: // xxx.aspx</link>
<!-- 内容简要描述 -->
< description >
How do Americans get ready to work with Russians aboard the
International Space Station ? They take a crash course in culture, language
and protocol at Russia ' s Star City.
</ description >
<!-- 发布时间 -->
< pubDate > Tue, 03 Jun 2003 09 : 39 : 21 GMT </ pubDate >
<!-- 目录 -->
< category > IT </ category >
<!-- 作者 -->
< author > bill </ author >
</ item >
</ channel >
</ rss >
三、创建RSS源
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//
1、创建RSS项目
SyndicationFeed feed = new SyndicationFeed( " Test Feed " , " This is a test feed " , new Uri( " http://Contoso/testfeed " ));
feed.Authors.Add( new SyndicationPerson( " TestFeederOne " ));
feed.Categories.Add( new SyndicationCategory( " Test News " ));
feed.Description = new TextSyndicationContent( " Technical News demo for RSS and ATOM publishing via WCF " );
feed.LastUpdatedTime = DateTime.Now.ToLocalTime();
// 2、创建RSS 中一个条目
SyndicationItem item1 = new SyndicationItem(
" Test Item " ,
" The first big steps on the road to overhauling the net's core addressing system have been taken. On Monday the master address books for the net are being updated to include records prepared in a new format known as IP version 6 " ,
new Uri( " http://Contoso/ItemOne " ),
System.Guid.NewGuid().ToString(),
DateTime.Now.ToLocalTime());
// 3、Adding the items to the list of generic syndication items.
List < SyndicationItem > items = new List < SyndicationItem > ();
items.Add(item1);
feed.Items = items;
SyndicationFeed feed = new SyndicationFeed( " Test Feed " , " This is a test feed " , new Uri( " http://Contoso/testfeed " ));
feed.Authors.Add( new SyndicationPerson( " TestFeederOne " ));
feed.Categories.Add( new SyndicationCategory( " Test News " ));
feed.Description = new TextSyndicationContent( " Technical News demo for RSS and ATOM publishing via WCF " );
feed.LastUpdatedTime = DateTime.Now.ToLocalTime();
// 2、创建RSS 中一个条目
SyndicationItem item1 = new SyndicationItem(
" Test Item " ,
" The first big steps on the road to overhauling the net's core addressing system have been taken. On Monday the master address books for the net are being updated to include records prepared in a new format known as IP version 6 " ,
new Uri( " http://Contoso/ItemOne " ),
System.Guid.NewGuid().ToString(),
DateTime.Now.ToLocalTime());
// 3、Adding the items to the list of generic syndication items.
List < SyndicationItem > items = new List < SyndicationItem > ();
items.Add(item1);
feed.Items = items;
// 4、Processing and serving the feed according to the required format
XmlWriter rssWriter = XmlWriter.Create(Server.MapPath( " RSS.XML " ));
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(rssWriter);
rssWriter.Close();
四、读取RSS中的内容
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
<summary>
/// 读取RSS内容
/// </summary>
private void ReadRss()
{
// TextSyndicationContent
Rss20FeedFormatter feed = new Rss20FeedFormatter();
// 1、得到RSS源路径
string sRssString = GetFullyQualifiedUrl( " rss.xml " );
// 2、读取RSS中的内容
using (XmlReader xreader = XmlReader.Create(sRssString))
{
feed.ReadFrom(xreader);
}
lblTitle.Text = feed.Feed.Title.Text;
if (feed.Feed.Copyright != null )
{
lblCopyright.Text = feed.Feed.Copyright.Text;
}
// 3、显示RSS中的内容
repeater1.DataSource = feed.Feed.Items;
repeater1.DataBind();
}
/// <summary>
/// 得到虚拟目录地址(与本程序无关)
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private string GetFullyQualifiedUrl( string url)
{
string sUrl1 = Request.Url.GetLeftPart(UriPartial.Authority);
string sUrl2 = ResolveUrl(url);
return string .Concat(Request.Url.GetLeftPart(UriPartial.Authority), ResolveUrl(url));
}
/// 读取RSS内容
/// </summary>
private void ReadRss()
{
// TextSyndicationContent
Rss20FeedFormatter feed = new Rss20FeedFormatter();
// 1、得到RSS源路径
string sRssString = GetFullyQualifiedUrl( " rss.xml " );
// 2、读取RSS中的内容
using (XmlReader xreader = XmlReader.Create(sRssString))
{
feed.ReadFrom(xreader);
}
lblTitle.Text = feed.Feed.Title.Text;
if (feed.Feed.Copyright != null )
{
lblCopyright.Text = feed.Feed.Copyright.Text;
}
// 3、显示RSS中的内容
repeater1.DataSource = feed.Feed.Items;
repeater1.DataBind();
}
/// <summary>
/// 得到虚拟目录地址(与本程序无关)
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private string GetFullyQualifiedUrl( string url)
{
string sUrl1 = Request.Url.GetLeftPart(UriPartial.Authority);
string sUrl2 = ResolveUrl(url);
return string .Concat(Request.Url.GetLeftPart(UriPartial.Authority), ResolveUrl(url));
}
五、源码
六、转载地址
http://www.cnblogs.com/scottckt/archive/2009/03/26/1422577.html