在ASP.NET(C#)中创建动态RSS提要页面 - 我是否需要做额外的事情?

Max*_*sky 4 c# xml asp.net iis rss

我希望创建一个动态RSS提要来表示我的网站的内容.目前,我有一个XML文件,其中每个主条目都包含文件的位置,日期和摘要数据.如果我要在ASP.NET中创建这个feed,除了解析XML和输出一些RSS之外,还需要做什么额外的事情吗?例如,我如何能够创建具有不同扩展名的ASP.NET页面,例如标准的RSS文件扩展名?

换句话说,假设我可以获得正确的RSS代码并通过Response.Write输出.虽然使用标准的RSS文件扩展名,但我如何确保它仍然作为ASP.NET应用程序运行?

Meh*_*hin 5

如果您使用的是.Net Framework 3.5,那么可以很好地生成RSS和Atom.检查以下MSDN页面.

http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx

或者您可以手动创建它,您必须实现RSS规范.

http://cyber.law.harvard.edu/rss/rss.html

或使用某些.NET工具,如RSS.NET.

http://www.rssdotnet.com/

要处理您自己的扩展并生成RSS,您必须创建一个HttpHandler并在IIS应用程序映射中添加扩展.

using System;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Web;
using System.Xml;
using System.Xml.Linq;

public class RSSHandler : IHttpHandler
{

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        XDocument xdoc = XDocument.Load("Xml file name");

        SyndicationFeed feed = new SyndicationFeed(from e in xdoc.Root.Elements("Element name")
                                                   select new SyndicationItem(
                                                       (string)e.Attribute("title"),
                                                       (string)e.Attribute("content"),
                                                       new Uri((string)e.Attribute("url"))));

        context.Response.ContentType = "application/rss+xml";

        using (XmlWriter writer = XmlWriter.Create(context.Response.Output))
        {
            feed.SaveAsRss20(writer);
            writer.Flush();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这只是一个示例,您必须设置其他一些Feed设置.