Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / XML

Creating an RSS 2.0 feed with .NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
27 Dec 2012CPOL2 min read 13.7K   5   3
A programming with .NET article with code of how to create a compliant RSS 2.0 feed using .NET XmlDocument

Overview

Rather than post another article about setting up and using third party tools with .NET MVC, I thought I would take a slightly different approach this time and write a programming with .NET article based on something I have had to do whilst creating Jambr.

I had the requirement to create an RSS feed for both the Articles and News sections of the site so you lovely readers could subscribe to either of them, I haven't actually had to create RSS feeds before so had to do some digging to find the best route to go down. I read numerous programming articles on line and compiled a simple class which enables me to create an RSS2.0 compliant feed, as seen here.

Using the Code

Rather than putting all 237 lines of code in here, I have uploaded the class for you, you can download it from here. Just pop it into your project and use it like this:
  1. Firstly, you need to create an instance of the object (apologies if I'm teaching you to suck eggs!):
    VB.NET
    Dim rssfeed As New RSSFeed()
  2. Next, you need to create the channel. RSS2.0 feeds can only have one channel so you're only able to call this method once:
    VB.NET
    rssfeed.CreateChannel("Jambr - News"
    "Http://www.jambr.co.uk/News",
    "Jambr News",
    Now,
    "en-GB")
  3. And now, you add your items. Obviously, you need to loop through the objects you want in your feed and add them, but we'll add just one example here:
    VB.NET
    rssfeed.WriteRSSItem("This is an item",
    "http://www.yoursite.com",
    "Karl",
    Now,
    "This is the description",
    Guid.NewGuid.ToString)
  4. Finally, after you've added your items, you can return the string of the XML document. I'm coding around .NET MVC 4 so as a result, I return the XML document to the user like this:
    VB.NET
    Return Content(rssfeed.ToString, "text/xml")

It is worth noting that I have included additional parameters on both the CreateChannel and WriteRSSItem methods which enables you to add Categories (in the context of Jambr, I tag all articles to enable them to be categorised) and Content (which will be added to the content:encoded tag). To add category tags, pass them as an array of KeyValuePair(of String, String) objects. To add the content, pass it as a string.

VB.NET
rssfeed.WriteRSSItem("This is an item",
"http://www.yoursite.com",
"Karl",
Now,
"This is the description",
Guid.NewGuid.ToString,
"This is the full body of the post",
{New KeyValuePair(Of String, String)("Tag1", "/Articles/?Tag=Tag1")})

A Few Issues I Encountered

There were a few issues I encountered whilst trying to ensure the feed was RSS 2.0 compliant which are sorted in this code, for example:

  1. Date Times needed to be in the correct format (Sat, 07 Sep 2002 9:42:31 GMT), luckily .ToString("r") on a datetime object handles that.
  2. Adding custom namespaces to the root rss element of the document, and then actually enabling me to write tags which reference that namespace. For example, <dc:creator> tags.

I hope that helps! Any questions, please ask.

This article was originally posted at http://www.jambr.co.uk/Article/create-rss2-feed

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Hewlett Packard Enterprise Security Services
United Kingdom United Kingdom
Technical Architect for Hewlett-Packard Enterprise Security Service.

Please take the time to visit my site

Comments and Discussions

 
Questionsystem.servicemodel.syndication namespace Pin
Clinton Gallagher27-Dec-12 9:17
professionalClinton Gallagher27-Dec-12 9:17 
// .NET developers interested in Web feeds...

1st boogle* this search term: system.servicemodel.syndication

2nd open Reflector and read the system.servicemodel.syndication namespace

The complete RSS 2.0 specification is supported including Atom but no native support for Media RSS which would require using system.servicemodel.syndication to write new classes.

NOTE: since adding this namespace Microsoft has moved it twice. It has been relocated in Windows 8 and I don't remember where it got relocated to so your verison of the framework may vary.

* boogle use bing or google search engine
clintonG

AnswerRe: system.servicemodel.syndication namespace Pin
Karl Stoney27-Dec-12 11:47
Karl Stoney27-Dec-12 11:47 
GeneralRe: system.servicemodel.syndication namespace Pin
Clinton Gallagher27-Dec-12 13:55
professionalClinton Gallagher27-Dec-12 13:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.