cs代碼
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection cn=new SqlConnection("server=.;database=company;uid=sa;pwd=sa");
SqlDataAdapter da=new SqlDataAdapter("select * from news",cn);
DataSet ds=new DataSet();
da.Fill(ds);
this.rptRss.DataSource=ds;
this.rptRss.DataBind();
}
//格式化文檔成xml,替換一些字串
protected string FormatForXml(object input)
{
string data=input.ToString();
data=data.Replace("&","&");
data=data.Replace("/",""");
data=data.Replace("'","&qapos;");
data=data.Replace("<","<");
data=data.Replace(">",">");
return data;
}
用Repeater綁定資料,注意下面aspx檔案的格式,html的代碼都沒有的,切忌
<%@ Page language="c#" Codebehind="myRssFeed.aspx.cs" ContentType="text/xml" AutoEventWireup="false" Inherits="test.myRssFeed" %>
<asp:Repeater id="rptRss" runat="server">
<HeaderTemplate>
<rss version="2.0">
<channel>
<title>rss測試</title>
<link>www.sina.com.cn</link>
<description>這是一個rss的測試</description>
</HeaderTemplate>
<ItemTemplate>
<item>
<title>
<%# FormatForXml(DataBinder.Eval(Container.DataItem,"title"))%>
</title>
<description>
<![CDATA[ <%# FormatForXml(DataBinder.Eval(Container.DataItem,"content"))%>]]>
</description>
<link>
<%# "http://localhost/CompanySystem/shownewsdetails.aspx?newsID="+FormatForXml(DataBinder. (Container.DataItem,"newsID"))%>
</link>
<author><%# FormatForXml(DataBinder.Eval(Container.DataItem,"author"))%></author>
<pubDate><%# FormatForXml(DataBinder.Eval(Container.DataItem,"addTime"))%></pubDate>
<category><%# FormatForXml(DataBinder.Eval(Container.DataItem,"className"))%></category>
</item>
</ItemTemplate>
<FooterTemplate>
</channel></rss>
</FooterTemplate>
</asp:Repeater>
例:
<?xml version="1.0" encoding="gb2312" ?>
<xml>
<asp:Repeater id="r" runat="server">
<itemtemplate>
<item id="0" title="ASP.NET發布RSS" url="http://127.0.0.1/1.html" image="http://127.0.0.1/1.jpg" />
</itemtemplate>
</asp:Repeater>
</xml>
後台.cs:
Response.ContentType = "text/xml";
Repeater.資料來源。。。
綁定。。。