天天看點

用ASP.NET建立一個線上RSS新聞聚合器(2)

<rss version="2.0">

  <channel>

  <title>Latest DataWebControls.com FAQs</title>

  <link>http://datawebcontrols.com</link>

  <description>

  This is the syndication feed for the FAQs

  at DataWebControls.com

  </description>

  <item>

  <title>Working with the DataGrid</title>

  <link>http://datawebcontrols.com/faqs/DataGrid.aspx</link>

  <pubDate>Mon, 07 Jul 2003 21:00:00 GMT</pubDate>

  </item>

  <item>

  <title>Working with the Repeater</title>

  <description>

  This article examines how to work with the Repeater

  control.

  </description>

  <link>http://datawebcontrols.com/faqs/Repeater.aspx</link>

  <pubDate>Tue 08 Jul 2003 12:00:00 GMT</pubDate>

  </item>

  </channel>

  </rss>

    關于<pubDate>元素的格式有一點特别重要,再此要講一下。RSS 要求日期必須按照 RFC822 日期和時間規範 進行格式化,此格式要求:開頭是一個可選的3字母星期縮寫加一個逗号,接着必須是日加上3字母縮寫的月份和年份,最後是一個帶時區名的時間。另外,要注意 <description> 子元素是可選的:上 述檔案第一個新聞沒有 <description> 元素,而第二個新聞就有一個。

    通過 ASP.NET 頁面輸出聚合内容

    現在,我們已經知道了如何按照 RSS2.0 規範存儲我們的新聞項,我們已經就緒建立一個 ASP.NET 頁面,當使用者送出請求時,就會傳回網站聚合 的内容。更确切地說,我們将建立一個名字叫 rss.aspx 的 ASP.NET 頁面,這個頁面會按照 RSS2.0 規範的格式傳回 Articles 資料庫表中的最新的 5 個新聞項 。

    可以有幾種方法來完成這件事,稍後将會講到。但是現在,我們首先要完成一件事,那就是先要從資料庫中獲得最新的5個新聞項。這可以用下面的 SQL 查詢語句獲得:

  SELECT TOP 5 ArticleID,Title,Author,Description,DatePublished FROM Articles ORDER BY DatePublished DESC

    獲得了這些資訊以後,我們需要把這些資訊轉換成相應的 RSS2.0 格式聚合檔案。要把資料庫的資料顯示為XML資料最簡單、快速的方法就是使用 Repeater 控件。準确地說,Repeater 控件 将在 HeaderTemplate 和 FooterTemplate 模版裡顯示<rss>元素、<channel>元素以及站點相關的 元素标簽,在 ItemTemplate 模版裡面顯示 <item> 元素。下面是我們這個 ASP.NET 頁面(.aspx檔案)的 HTML 部分 :

  <%@ Page language="c#" ContentType="text/xml" Codebehind="rss.aspx.cs"

  AutoEventWireup="false" Inherits="SyndicationDemo.rss" %>

  <asp:Repeater id="rptRSS" runat="server">

  <HeaderTemplate>

  <rss version="2.0">

  <channel>

  <title>ASP.NET News!</title>

  <link>http://www.ASPNETNews.com/Headlines/</link>

  <description>

  This is the syndication feed for ASPNETNews.com.

  </description>

  </HeaderTemplate>

  <ItemTemplate>

  <item>

  <title><%# FormatForXML(DataBinder.Eval(Container.DataItem,

  "Title")) %></title>

  <description>

  <%# FormatForXML(DataBinder.Eval(Container.DataItem,

  "Description")) %>

  </description>

  <link>

  http://www.ASPNETNews.com/Story.aspx?ID=<%#

  DataBinder.Eval(Container.DataItem, "ArticleID") %>

  </link>

  <author><%# FormatForXML(DataBinder.Eval(Container.DataItem,

  "Author")) %></author>

  <pubDate>

  <%# String.Format("{0:R}",

  DataBinder.Eval(Container.DataItem,

  "DatePublished")) %>

  </pubDate>

  </item>

  </ItemTemplate>

  <FooterTemplate>

  </channel>

  </rss>

  </FooterTemplate>

  </asp:Repeater>

    首先要注意的是:上面這段代碼例子隻包括 Repeater 控件,沒有其它的 HTML 标記或 Web 控件。這是因為我們希望頁面隻輸出 XML 格式的資料。實際上,觀察一下 @Page 指令,你就會發現 ContentType 被設定為XML MIME 類型(text/xml)。其次要注意的是:在 ItemTemplate 模版裡,當 在 XML 輸出中添加資料庫字段Title、Description 和 Author 時,我們調用了輔助函數 FormatForXML()。我們 很快就會看到,該函數被定義在背景編碼的類中,其作用隻是将非法的 xml 字元替換為它們對應的合法的轉義字元。最後我們應該注意,在 <pubDate> 元素裡面的資料庫字段 DatePublished 是用 String.Format 來格式化的。标準的格式描述符“R”對 DatePublished 的值進行相應的格式化 。