I have a Master’s degree in computer science and a bachelor's degree in applied mathematics. I’m also known to be a technical author with several technical publications in the form of books and articles. Besides, I'm an open source enthusiast and have coordinated or contributed to several projects. Currently, I maintain my projects on GitHub.
As a content provider on the internet, not only I publish on this technical blog, but also I'm a podcaster and publish audio podcasts on Mash This.
Trying to maintain a healthy and active lifestyle, I'm a pescetarianist and exercise almost everyday. I’m an avid runner, soccer defender, and tennis player. I also have an interest in fashion.
Last week after finishing manuscripts of my book, I promised to get back to regular blogging and give more stuff about programming and .NET especially about newly released .NET 3.5 so here you go! It's been a long time since the last time that I sent a blog post with programming codes, isn't it?!
One of very nice additions to .NET 3.5 is the Syndication library for Windows Communication Foundation which lets you create RSS and Atom feeds and deal with data from existing feeds easily. You know that there were some good components for this purpose but now that we have a built-in feature for this then .NET developers can write consistent codes for their feeds.
On the other hand, LINQ is a new feature in .NET 3.5 that should be used in any .NET 3.5 application that deals with data.
Applying these two features together, we can fetch data from different data storage systems and create RSS or Atom feeds with syndication library in WCF which is the topic that will be covered in this post. I show this by giving an example.
Before stepping in the process of doing this, I create an ASP.NET 3.5 Web Application project.
First of all, I create a very simple database with a single table that keeps data for my blog posts. It has some columns for ID, title, Body, Date and Category and I save some sample data into this table.
Now I add a LINQ to SQL item to my project and add my table to this item as well as another entity for my post categories because I want to select them easily and add them to my feed.
Before implementing my code, I need to add a reference to System.ServiceModel.Web assembly to my project because it includes the syndication library of WCF in.NET Framework 3.5.
The main part is where I implement my feed logic. I use my Default.aspx page to display the feed content. The C# code to do this is shown below.
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
DataClassesDataContext dataContext = new DataClassesDataContext();
// Select data
var itemsQuery = from post in dataContext.Posts
// Prepare response
Response.Buffer = false;
Response.ContentType = "application/xml";
// Create an XmlWriter to write the feed into it
using (XmlWriter writer = XmlWriter.Create(Response.OutputStream))
// Set the feed properties
SyndicationFeed feed = new SyndicationFeed
"Daily musings about .NET.",
// Add authors
// Add categories
var categoriesQuery = from cat in dataContext.Cats
foreach (var category in categoriesQuery)
// Set copyright
feed.Copyright = new TextSyndicationContent
("© Copyright 2005-2007 Keyvan Nayyeri");
// Set generator
feed.Generator = "Keyvan's very own RSS Generator :-D";
// Set language
feed.Language = "en-US";
// Add post items
List<SyndicationItem> items = new List<SyndicationItem>();
foreach (var Post in itemsQuery)
SyndicationItem item = new SyndicationItem();
item.Id = Post.ID.ToString();
item.Title = TextSyndicationContent.CreatePlaintextContent(Post.Title);
item.Content = SyndicationContent.CreateXhtmlContent(Post.Body);
item.PublishDate = Post.Date;
feed.Items = items;
// Write the feed to output
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
All the logic is implemented in the Load event of the page. First I wrote a query for my blog post items and then prepared my Response object to display XML data for my feed.
WCF syndication uses XmlReader and XmlWriter objects to read or write data for RSS 2.0 or Atom 1.0 feeds. So I created an XmlWriter based on the Response output stream.
You can write SyndicationFeed or SyndicationItem to the output. I created a SyndicationFeed as the root and container object for all my feed data and set some properties for my feed. At the next step I added an author to my feed by using SyndicationPerson object instance. In the next step, I created another query to fetch categories list and iterated through these categories and added them to my feed categories list via SyndicationCategory object instance.
Setting some properties like copyright, generator name and language was the next step and then I created a list of SyndicationItem objects. This class represents a single blog post that provides appropriate properties for this purpose. Iterating through my blog posts and adding them to the list was the last step of fetching data and adding them to my feed objects.
At the end, I used Rss20FeedFromatter to write my feed data into the XmlWriter and showing them to the end user. I can simply replace this Rss20FeedFormatter with Atom10FeedFormatter to generate an Atom 1.0 feed.
Now I can simply view my page in the browser to see my generated feed.
You can download the source code sample for this post from here.