asp.net生成RSS

时间:2021-05-06 16:04:40

经常看到博客、还有很多网站中有RSS订阅,今天就来玩玩asp.net生成RSS,在网上查找了相关资料 发现just soso,如下:

aspx

<?xml version="1.0" encoding="utf-8"?>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RSSDemo.aspx.cs"  ContentType="text/xml"  Inherits="WebApp1.RSSDemo"%>
<rss version="2.0">
    <channel>
        <title>网上书店</title>
        <description>书店最新发表的图书列表</description>
    <asp:Repeater runat="server" ID="RepeatRSS">
        <ItemTemplate>
        <item>
            <title><![CDATA[<%#Eval("Title") %>]]></title>
                <link><![CDATA[<%#Eval("ID","http://localhost:2453/WebForm1.aspx?id={0}") %>]]></link>
             <description><![CDATA[<%#Eval("Descript") %>]]></description>
             <pubDate><![CDATA[<%#Eval("PubDate") %>]]></pubDate>
        </item>
    </ItemTemplate>
    </asp:Repeater>
    </channel>
</rss>

aspx.cs文件

namespace WebApp1
{
    public partial class RSSDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

    ///一般这里会从数据库中查询数据,这里为了方便阅读就将就着用了
            List<BookS> listBook = new List<BookS>() {
                new BookS(){Title="ASP.NET",Descript="详解",ID="1",PubDate=DateTime.Now.AddMonths(1)},
                new BookS(){Title="SqlServer",Descript="详解",ID="2",PubDate=DateTime.Now.AddMonths(-2)},
                new BookS(){Title="Oracle",Descript="详解",ID="3",PubDate=DateTime.Now.AddDays(-11)},
                new BookS(){Title="Ajax",Descript="详解",ID="4",PubDate=DateTime.Now.AddDays(10)},
                new BookS(){Title="SilverLight",Descript="详解",ID="5",PubDate=DateTime.Now},
            };
            RepeatRSS.DataSource = listBook;
            RepeatRSS.DataBind();
        }
    }

///实体类
    public class BookS
    {
        public string Title { set; get; }
        public string Descript{ set; get; }
        public string ID { set; get; }
        public DateTime PubDate { set; get; }
    }
}