Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating dynamic RSS feed page in ASP.NET (C#) - do I need to do anything extra?

Tags:

c#

xml

asp.net

iis

rss

I wish to create a dynamic RSS feed to represent my site's content. Currently, I have an XML file where each main entry has data for location, date, and summary of a file. If I were to create this feed in ASP.NET, would I need to do anything extra, in terms of things other than just parsing the XML and outputting some RSS? For example, how would I be able to create an ASP.NET page with a different extension, such as the standard RSS file extension?

In other words, let's say I can obtain the proper RSS code and output it through Response.Write. How do I make sure that it still functions as a ASP.NET application, though with the standard RSS file extension?

like image 317
Maxim Zaslavsky Avatar asked Jan 24 '23 00:01

Maxim Zaslavsky


2 Answers

If you are using .Net Framework 3.5, there is a great facility to generate RSS and Atom. Check the following MSDN page out.

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

Or you can create it manually which you must implement the RSS specification.

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

Or using some .NET Tool such as RSS.NET.

http://www.rssdotnet.com/

For handling your own extension and generating RSS, you have to create an HttpHandler and add the extension in the IIS application mapping.

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();
        }
    }

}

It's only a sample and you have to set some another settings of feed.

like image 62
Mehdi Golchin Avatar answered Jan 27 '23 13:01

Mehdi Golchin


Try making a Custom HTTPHandler. Add a custom extension to this handler in web.config and then add this to IIS so that this can be served by IIS.

This HTTPHandler will do the RSS processing from XML and can write the output to your reponse.

This might be of help: http://msdn.microsoft.com/en-us/library/ms972953.aspx

like image 35
Ashish Jain Avatar answered Jan 27 '23 12:01

Ashish Jain