我爱Aspx >> C#.Net >> 用ASP.NET建立一个在线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> |
ASP.NET应用程序中调用EJB[04-30]
ASP.NET 2.0中使用webpart系列控..[04-30]
ASP.NET 2.0轻松实现数据库应用开..[04-30]
漫谈ASP.NET设计中的性能优化问题[04-30]
VB.Net编程实现Web Service的基础[04-30]
Visual Basic .NET处理Excle表格..[04-30]
VB.NET利用OBEX协议实现红外线文..[04-30]
Visual Basic .Net打造个性化菜单[04-30]
ASP.NET2.0导航功能之配置会员和..[04-30]
揭开.NET消息循环的神秘面纱[04-30]
ASP.NET应用程序中调用EJB[04-30]
ASP.NET 2.0中使用webpart系列控..[04-30]
ASP.NET 2.0轻松实现数据库应用开..[04-30]
漫谈ASP.NET设计中的性能优化问题[04-30]
用Avalon建立未来的Windows用户界..[04-30]
继往开来 Visual Baisc 2005初体..[04-30]
VB.Net编程实现Web Service的基础[04-30]
在VS2005中打造自己的Starter Ki..[04-30]
Visual Basic .NET处理Excle表格..[04-30]
VB.NET利用OBEX协议实现红外线文..[04-30]