首先设计好数据库,然后开始做页面。使用Repeater控件,代码如下:ReadRss.aspx
<%@ Page language="c#" Codebehind="ReadRss.aspx.cs" AutoEventWireup="false" Inherits="MyTest.rss" ContentType="text/xml" %>
<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.Eval(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>
后置cs文件代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
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;
}
public void BindData()
{
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);
rptRss.DataSource=ds;
rptRss.DataBind();
}
注意要用FormatForXml来格式化,否则输出不符合XML格式,会导致失败!