ASP.NET 發布 RSS

來源:互聯網
上載者:User

 

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("<","&lt;");
    data=data.Replace(">","&gt");
    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.資料來源。。。
綁定。。。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.