<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.as.netnews.com/headlines/</link>
<description>
this is the syndication feed for as.netnews.com.
</description>
</headertemplate>
<itemtemplate>
<item>
<title><%# formatforxml(databinder.eval(container.dataitem,
"title")) %></title>
<description>
<%# formatforxml(databinder.eval(container.dataitem,
"description")) %>
</description>
<link>
http://www.as.netnews.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 的值进行相应的格式化 。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器