Up to this point my blog engine has been working quite nicely; however, it does lack one key feature: an RSS feed. Without support for RSS people, will have to continually come back to my website to check for new posts. That's not likely to occur :( So let's fix this and get an RSS feed up and running!

RSS is widely known as Really Simple Syndication and is a standardised format that allows publishers (blog owners) to syndicate data (posts) automatically.

Think of it like this. There are two basic ways I can get the latest newspaper: go to the local newsagent or get it delivered to my house. RSS is the equivalent of getting the newspaper delivered to my house. It gets 'syndicated' to me whenever the latest copy is available.

There are a number of standardised formats for an RSS feed but at their most basic level they are an XML file. Knowing this means we have a number of options available for constructing our feed.

Option 1. Manually Construct

Because it's XML we could just concatenate together a bunch of strings wrapping them in the correct XML tags.

If you head over to the rss advisory board you can read through the RSS 2.0 specification. There you will find the structure for the XML file and its elements. Then it's just a matter of concatenating strings of data and XML together.

It doesn't take a rocket scientist to know that this will get messy very quickly and is susceptible to bugs! So for obvious reasons we will not use this method.

Option 2. Serialize a Class Model

Seeing as we have the RSS 2.0 specification we could create our own class model. Then it'd just be a matter of serializing to XML and bingo, it's done. However once again this requires creating a lot of infrastructure code and processes.

There has to be an easier way.

Option 3. Use the System.ServiceModel.Syndication namespace

As luck would have it there is!

Check out theSystem.ServiceModel.Syndication namespace in .NET. The classes of particular interest are:

  • SyndicationFeed provides information about your blog.
  • SyndicationLink provides information about links.
  • SyndicationItem provides information about blog posts.

Here's a quick and dirty example using my blog. View the github page to see the proper class.

 var feed = new SyndicationFeed();

// Add basic blog information
feed.Id = pbaseUrl;
feed.Title = new TextSyndicationContent("Kestrel Blackmore Blog");
feed.Description = new TextSyndicationContent("Hi, I'm Kestrel. I've been a software developer now for 10+ years and I love it!");
feed.Copyright = new TextSyndicationContent("Kestrel Blackmore");
feed.LastUpdatedTime = new DateTimeOffset(DateTime.Now);
feed.Generator = "BlogMatrix 1.0";
feed.ImageUrl = new Uri(pbaseUrl + "/assets/img/blackmore_logo.png");

// Add the URL that will link to your published feed when it's done
SyndicationLink link = new SyndicationLink(new Uri(pbaseUrl + "/feed.xml"));
link.RelationshipType = "self";
link.MediaType = "text/html";
link.Title = "Kestrel Blackmore Feed";
feed.Links.Add(link);

// Add your site link
link = new SyndicationLink(new Uri(pbaseUrl));
link.MediaType = "text/html";
link.Title = "Kestrel Blackmore Blog";
feed.Links.Add(link);

// get blog posts
var items = new List();

foreach(var post in myPosts) {
    var item = new SyndicationItem();
    item.Title = TextSyndicationContent.CreatePlaintextContent(post.Value.title);
    item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(pbaseUrl + post.Value.link.url)));
    item.Summary = TextSyndicationContent.CreateHtmlContent(post.Value.summary);
    var date = DateTime.Parse(post.Value.publish_date);
    item.PublishDate = new DateTimeOffset(date);
    item.Authors.Add(new SyndicationPerson(post.Value.author.email, post.Value.author.name, post.Value.author.url));
    items.Add(item);
}

feed.Items = items;

The above will create the required objects for an RSS feed. Now we need an endpoint so the feed can be consumed.

Creating the RSS endpoint

Creating the endpoint is as simple as writing some XML to Response.Output and setting the relevant Response.ContentType.

Here's the code (Remember I'm using WebMatrix with Razor Pages so YMMV):

var feedWriter = XmlWriter.Create(Response.OutputStream);

// feed object is from previous code example
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
rssFormatter.WriteTo(feedWriter);

Response.ContentType = "application/rss+xml";

feedWriter.Close();

Note the use of the Rss20FeedFormatter class? We could have used an Atom10FeedFormatter instead. Check out the System.ServiceModel.Syndication namespace for more formatter options.

Conclusion

Creating an RSS feed with C# is pretty darn easy! To see the full code listing view my BloxMatrix github repository and check out the code in:

App_Code\RssSyndicator.cs
feed.cshtml

Next post I'll be looking at how I get the blog post information (I don't use a database for my blog engine) for my RSS feed and how I used this to also refactor my Home and Archives pages.



comments powered by Disqus