关于如何制作Rss的一种方法!

正好自己的项目上需要生成RSS, 到处找了一些资料,这个比较浅显,转到自己的blog上来,也算是给自己留个地方!

    很多朋友问到很多网页上面都有一个的标志,为什么这样呢?用来有什么用呢?怎么做呢?好,下面我们就简单的分析一下!站点用来和其他站点之间共享内容的简易方式(也叫聚合内容)。 RSS使用XML作为彼此共享内容的标准方式。用处就不多说了,大家关心的还是怎么做!

首先数据库设计,如图:

好,下面我们开始做页面,我使用的是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("&","&amp;");
   data=data.Replace("/","&quot;");
   data=data.Replace("'","&qapos;");
   data=data.Replace("<","&lt;");
   data=data.Replace(">","&gt");
   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格式,会导致失败!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值