ASP.NET 发布 RSS

 

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.数据源。。。
绑定。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值