asp.net 2.0中生成RSS
前台代码:
<%@ Page Language=”C#” AutoEventWireup=”true” ContentType=”text/xml” CodeFile=”MainFeed.aspx.cs” Inherits=”test2″ ValidateRequest=”false” StylesheetTheme=”" Theme=”" EnableViewState=”false” ResponseEncoding=”UTF-8″%>
<asp:Repeater ID=”RptRSS” runat=”server”>
<HeaderTemplate>
<rss version=”2.0″>
<channel>
<title>纪检监督</title>
<link>http://localhost/</link>
<description>
纪检监督信息网新闻排行
</description>
</HeaderTemplate>
<ItemTemplate>
<item>
<title><%#FormatForXML(Eval(”Title”)) %></title>
<link><%#Eval(”NewsUrl”) %></link>
<pubDate><%#Convert.ToDateTime(Eval(”ApprovedDate”).ToString()).ToString(”r”) %></pubDate>
<description><%#FormatForXML(Eval(”Abstract”))%></description>
<author><%#FormatForXML(Eval(”PostUser”))%></author>
</item>
</ItemTemplate>
<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataView myDV = Inspection.WebModules.NewsManager.Business.News.GetTopNewsList().Tables[0].DefaultView;//根据自己的要求设置
RptRSS.DataSource = myDV;
RptRSS.DataBind();
}
protected string FormatForXML(object input)
{
string data = input.ToString(); // cast the input to a string
// replace those characters disallowed in XML documents
data = data.Replace(”&”, “&”);
data = data.Replace(”\”", “"”);
data = data.Replace(”‘”, “'”);
data = data.Replace(”>”, “>”);
return data;
}
}
来源:http://steptry.wordpress.com/2005/12/20/aspnet-20%E4%B8%AD%E7%94%9F%E6%88%90rss/