Click here to Skip to main content
13,256,012 members (47,453 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 26 Oct 2008

Generation of web feeds and podcasts

, 26 Oct 2008
Rate this:
Please Sign up or sign in to vote.
This article describes the generation of web feeds (RSS, Atom, podcasts) using wrapper classes.


The code attached to this article handles the creation of a web feed; either in the format of RSS 2.0 or Atom syndication. In this article, I will explain how to use the code.


A web feed is a syndication of content, that allows internet users to subscribe to the content and receive notification about new additions, or easily retrieve and remix the content which is provided in a common format.

A web feed can include files, e.g., video, sound, images, or PDF documents. Some feed readers/aggregators are able to handle the playback of sound and video. In iTunes, it is possible to subscribe to podcasts and retrieve new episodes automatically, e.g., new radio programs or TV shows in a series.

I set out to create a common class to handle the syndication of content. Along the way, I learned more about the standards RSS and Atom, and the requirements to make iTunes accept and successfully subscribe to a web feed. I tested the resulting feeds in an online validation tool here.

Using the Code

The code is written in C#, and consists of the four classes Feed, FeedItem, FeedEmailAddress, and FeedItemEnclosure.


The Feed class contains the main info about the feed, and a list of feed items. The FeedItem class represents a feed item that might also hold an enclosed file. The FeedItemEnclosure class represents the enclosed file itself, and the FeedEmailAddress class contains info about a person (name and email address).

The Feed class contains two methods, WriteRSS and WriteAtom, that create and write the web feeds in a given HttpContext.

These classes can be used in an ASP.NET website as described in the following section. Using Visual Studio, a website can be created by choosing File / New / Web Site. Alternatively, the demo website in the attached demo package contains it all.

Preparing the website

  • In the website, create the folder App_Code, if it does not already exist.
  • From the source package attached with this article, add the four *.cs-files with the four classes Feed, FeedItem, FeedEmailAddress, and FeedItemEnclosure to the App_Code folder.
  • Create a new web form, or a new generic handler. (Do so in Visual Studio by going to Solution Explorer and right clicking the website and choosing Add New Item. Then, choose either to create a web form (ASPX), or a generic handler (ASHX).)
  • In a web form, add the following code to the Page_Load event handler. In a generic handler, add the following code to the ProcessRequest method.

Using the feed classes

  1. In the Page_Load event handler of a web form (ASPX), or in the ProcessRequest method of a generic handler (ASHX), create an instance of the Feed class with the values for the required parameters title and description:
  2. Feed feed = new Feed("Northwind Employees", "Employees in the Northwind database");
  3. Then, I need some content to syndicate. It could be any list of content, e.g., a DataTable or a generic list of business objects. In this example, I use data from a SQL Server database, the Nortwhind database, and read all rows from the Employees table, as follows:
  4. string connectionString = 
        @"Data Source=.\SqlExpress;Initial Catalog=Northwind;Integrated Security=True";
    using (SqlConnection connection = new SqlConnection(connectionString))
        using (SqlCommand command = new SqlCommand(@"
            SELECT LastName, FirstName, Title, TitleOfCourtesy, HireDate, Notes 
                FROM Employees 
                ORDER BY HireDate DESC, LastName ASC"
            , connection))
            using (SqlDataReader reader = command.ExecuteReader())
                while (reader.Read())
                    // TODO: CREATE FEED ITEM (STEPS 3-5 BELOW)
                    // TODO: ADD FEED ITEM TO FEED (STEP 6 BELOW)

    Inside the inner loop above, I create and add each feed item, see steps 3-6 below.

  5. The feed item in this example is generated from the current row in the SqlDataReader. First, I format the data for the title of the feed item so it will be like: Ms. Anne Dodsworth, Sales Representative:

    string title = string.Format("{0} {1} {2}, {3}", 
      reader["TitleOfCourtesy"], reader["FirstName"], 
      reader["LastName"], reader["Title"]);
  6. Then, I create the feed item with the required title, and also provide the additional Description and PublishDate.
  7. FeedItem feedItem = new FeedItem(title)
        Description = (string)reader["Notes"],
        PublishDate = (DateTime)reader["HireDate"]
  8. If a file should be enclosed with the feed item, I attach it with a URL to the file, the MIME type of the file, and the size in bytes, as follows:
  9. string urlToFile = "http://nothing.nowhere/avatar.jpg";
    string mimeType = "image/jpeg";
    int sizeInBytes = 30000;
    feedItem.Enclosure = new FeedItemEnclosure(urlToFile, mimeType, sizeInBytes);
  10. Now, I have completed the feed item, and will add it to the feed:
  11. feed.Items.Add(feedItem);
  12. After looping through data, I'm ready to write the web feed to the current HttpContext. I choose to first create an RSS feed.

    For the web form (ASPX), the current HttpContext is the Context property of the Page:


    For the generic handler (ASHX), the current HttpContext is the context parameter of the ProcessRequest method:


    The generated RSS feed has the following XML content:

    <?xml version="1.0" encoding="utf-8" ?>
    <rss xmlns:itunes="" 
         xml:lang="" version="2.0">
        <title>Northwind Employees</title>
        <description>Employees in the Northwind database</description>
        <atom:link xmlns:atom="" rel="self" 
            type="application/rss+xml" />
        <pubDate>Sun, 26 Oct 2008 16:36:31 GMT</pubDate>
        <lastBuildDate>Sun, 26 Oct 2008 16:36:31 GMT</lastBuildDate>
        <itunes:subtitle>Employees in the Northwind database</itunes:subtitle>
        <itunes:summary>Employees in the Northwind database</itunes:summary>
          <title>Ms. Anne Dodsworth, Sales Representative</title>
            Anne has a BA degree in English from St. Lawrence College. 
            She is fluent in French and German.
          <pubDate>Tue, 15 Nov 1994 00:00:00 GMT</pubDate>
          <enclosure url="http://nothing.nowhere/avatar.jpg" 
             type="image/jpeg" length="30000" />
            Anne has a BA degree in English from St. Lawrence College. 
            She is fluent in French and German.
            Anne has a BA degree in English from St. Lawrence College. 
            She is fluent in French and German.
          <media:content url="http://nothing.nowhere/avatar.jpg" 
             fileSize="30000" type="image/jpeg" />
  13. Then, to create an Atom feed instead, I write:
  14. feed.WriteAtom(this.Context)

    The generated Atom feed has the following XML content:

    <?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="">
      <title type="html">Northwind Employees</title>
      <link rel="self" type="application/atom+xml" 
         href="http://localhost:51529/FeedDemoWebSite/default.aspx" />
      <subtitle type="html">Employees in the Northwind database</subtitle>
        <title>Ms. Anne Dodsworth, Sales Representative</title>
        <content type="html">
          Anne has a BA degree in English from St. Lawrence College. 
          She is fluent in French and German.
        <link rel="enclosure" type="image/jpeg" 
          length="30000" href="http://nothing.nowhere/avatar.jpg" />
  15. That's it. I have a feed (RSS or Atom) that users of my website can subscribe to:
  16. FeedGenerator1.jpg

The code in the classes

The source code is commented, and I hope that the comments will provide some answers to questions that may arise there. Therefore, I chose not to go into details with the classes here.

If you look at the code in the classes, I will be happy to hear about errors, improvements, suggestions, or other comments. I included some TODO comments as well that indicate points I might change in the future.

Points of Interest

Note that to create a podcast that users can subscribe to from iTunes, enclose a file that iTunes supports, and provide the correct MIME type, e.g., "video/mp4" or "audio/mp3". For a list of MIME types for iTunes, see the section "iTunes RSS Tags" on Apple's page about iTunes podcasts.


  • 26-10-2008 - Original article.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

Ole L. Sørensen
Software Developer
Denmark Denmark
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThank you soo much :) Pin
CoolFire4eva1-Feb-10 14:43
memberCoolFire4eva1-Feb-10 14:43 
GeneralWriting different feeds Pin
Matt_Izilla27-Oct-08 16:52
memberMatt_Izilla27-Oct-08 16:52 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 26 Oct 2008
Article Copyright 2008 by Ole L. Sørensen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid