rss現在很流行,它推行的一套xml格式的標準來傳輸資料,和webservice差不多,不過它必須遵從一套它自己的標準。
建立rss有很多種方式,最終的目的是形成一個xml文檔的輸出資料流,這個文檔要符合一定的標準格式。找到一種比較簡單的形成rss的方法,使用repeater來綁定重複的xml節點。
建立一個webform Rss.aspx,在html視圖中刪除除了第一行頁面聲明以外的其它代碼,寫上以下xml格式的代碼
1<%@ Page language="c#" Codebehind="Rss.aspx.cs" AutoEventWireup="false" Inherits="RSSReader.Rss" %>
2<?xml version="1.0" ?>
3<rss version="2.0">
4 <channel>
5 <title>測試rss</title>
6 <link>http://zqs.cnblogs.com/</link>
7 <description>測試rss</description>
8 <language>zh-cn</language>
9 <asp:Repeater id="Repeater1" runat="server">
10 <ItemTemplate>
11 <item>
12 <title><%#DataBinder.Eval(Container.DataItem,"sch_name")%></title>
13 <description><![CDATA[<%#DataBinder.Eval(Container.DataItem,"sch_name")%>]]></description>
14 <pubDate><%#((DateTime)DataBinder.Eval(Container.DataItem,"indate")).ToString("r") %></pubDate>
15 <link>http://zqs.cnblogs.com/show.aspx?id=<%# DataBinder.Eval(Container.DataItem, "sch_no") %></link>
16 </item>
17 </ItemTemplate>
18</asp:Repeater>
19</channel>
20</rss>
21
代碼說明:
建立一個xml格式的文檔,然後用Repeater來重複顯示資料行節點
該頁面的後置代碼檔案Rss.cs中的代碼如下,在該頁面的Page_Load事件中寫上如下代碼:
1 SqlConnection conn = new SqlConnection("server=.;database=test;uid=sa;pwd=;");
2
3 System.Data.SqlClient.SqlDataAdapter myComm = new SqlDataAdapter("select * from dSchool",conn);
4
5 DataSet ds = new DataSet();
6
7 myComm.Fill(ds,"Table1");
8
9 Response.ContentType="Text/XML";
10 Repeater1.DataSource = ds;
11
12 Repeater1.DataBind();
代碼說明:
使用Response.ContentType="Text/XML";將頁面的輸出資料流設定為xml格式,其它就是得到資料集綁定Repeater了