Over a million developers have joined DZone.

Rendering RSS feeds in .NET


I covered the process of reading RSS feeds in my previous article , but what if you want to create your own RSS feeds? Instead of directly working with XML creation, System.ServiceModel.Syndication can help the developers by providing some classes that organize the feed rendering process.

To show how to do this, I am creating a project that will get the feed data from a database and based on that, will render the feed. I created a simple SQL database with one table called posts that contains two columns – title and contents. Pretty self-descriptive, and should make the references in the feed clearer.

First of all, I need to get the needed feed data from the database. To do this, I am using the code below (you need to add a reference to the System.Data.SqlClient namespace) that basically executes a SQL query and retrieves the data from all columns, later returning a dictionary:public Dictionary<string, string> GetValues()

Dictionary<string, string> values = new Dictionary<string, string>();

using (SqlConnection connection = new SqlConnection(@"Data Source=DENNIS-PC\SQLEXPRESS;Initial Catalog=blogposts;User Id=dennis;Password=dennis;"))
string query = "SELECT * FROM posts";
SqlCommand command = new SqlCommand(query, connection);
using (SqlDataReader reader = command.ExecuteReader())
if (reader.HasRows)
while (reader.Read())
values.Add(reader.GetValue(0).ToString().Trim(), reader.GetValue(1).ToString().Trim());
return values;

Considering that the data is stored in a database, the process can be easily adapted to a real-world solution. The returned dictionary in this case will contain the post title as the key and the contents as the value. Now to the actual feed creation.

Same as with feed reading, feed creating is handled via SyndicationFeed. The returned result is passed to a Rss20FeedFormatter, that serializes the feed to RSS 2.0 format. Here is what the code looks like:

public Rss20FeedFormatter GetFeed(Dictionary<string,string> items)
SyndicationFeed feed = new SyndicationFeed("Feed Title", "Description", new Uri("http://myfeedurl"));

feed.Authors.Add(new SyndicationPerson("authoremail@domain", "Dennis", "http://userurl"));
feed.Categories.Add(new SyndicationCategory("Feed Category"));
feed.Description = new TextSyndicationContent("This is the feed description");

List<SyndicationItem> itemList = new List<SyndicationItem>();

foreach (string key in items.Keys)
SyndicationItem item = new SyndicationItem(key, items[key],new Uri("http://alternateurl"));

feed.Items = itemList;

return new Rss20FeedFormatter(feed);


As you can see here, I am creating a new instance of SyndicationFeed that sets the feed title, description and URL. I am using dummy URLs for testing purposes only, so you can change those accordingly. I am also setting the feed author, description and category. This helps later on when the user wants to identify your feed.

A feed is composed of a multitude of SyndicationItem instances; therefore I am passing the dictionary I created above as the parameter to the function and passing through each single entry to create a new syndication item. I am also adding every new item to a list that is later on assigned to the feed.Items property.

To test all the above code, you can use the following snippet:

Dictionary<string, string> values = GetValues();
Rss20FeedFormatter formatter = GetFeed(values);

foreach (SyndicationItem item in formatter.Feed.Items)


Once you get it up and running, you will see the item titles shown in the Output window in Visual Studio.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}