Rss應用(一) 建立rss

來源:互聯網
上載者:User

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了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.