Syndication命名空间实现RSS功能学习

一、Rss概述

RSS也叫聚合RSS是在线共享内容的一种简易方式(也叫聚合内容,Really Simple Syndication)。通常在时效性比较强的内容上使用RSS订阅能更快速获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新。

二、Rss格式

ExpandedBlockStart.gif RSS的格式
<!-- 以下是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源

ExpandedBlockStart.gif 创建RSS源
// 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;

        
// 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中的内容

ExpandedBlockStart.gif 读取RSS中的内容
///   <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));
    }

五、源码

/Files/S-TGM/RssSite.rar

六、转载地址

http://www.cnblogs.com/scottckt/archive/2009/03/26/1422577.html

转载于:https://www.cnblogs.com/S-TGM/archive/2010/02/02/1661624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值