用ASP.NET建立一个在线RSS新闻聚合器(2)

来源:互联网 发布:php传值和传引用的区别 编辑:程序博客网 时间:2024/05/05 21:47
<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 的值进行相应的格式化 。