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

     网上有许多人写RSS的创建方法。但总觉得不够周全。我将自己学习成果公布出来,供大家讨论。本文主要通过framework 3.5中的Syndication命名空间来实现。


本文大钢:

     一、了解RSS源
     二、RSS源格式
     三、如何创建RSS源
     四、如何订阅RSS源

     五、如何更新RSS源
     六、源码下载


 一、了解RSS源
    RSS:Really Simple Syndication。是一个标准化的XML标记,用于描述你想要分享的内容。

二、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>



三、如何用Syndication创建RSS源

    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中的内容
     创建了RSS想查看一下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));
    }

 注:查看其它网站的RSS方法同上。

五、如何更新RSS。

   当有新内容时,用户怎么知道有新的内容?此部分我没有仔细研究。我的思路是当有新内容增加时可以更新Rss文件,让用户知道RSS内容有变更。其它方法如将要更新的内容放入数据库等大家可以思考。这里不作说明。


六:源码下载

源码地址:https://files.cnblogs.com/scottckt/RssSite.rar

参考:http://www.cnblogs.com/JimmyZhang/archive/2007/09/14/892431.html
原文地址:https://www.cnblogs.com/scottckt/p/1422577.html