Using .Net WCF Syndication API to Read a Comments Feed
In the last few days I have started a migration process to my own domain
– www.gilfink.net. Currently this domain is redirecting to this blog but in
the near future I’ll move my blog to there. I’ll keep publishing in this blog along with the new one so don’t worry. One of the biggest problems I got stuck with was that this blog community server doesn’t include an export feature like in other blog platforms like BlogEngine.Net. So I’m currently building a migration tool to help me move my blog content. In this post I’ll show an example of how to read all the blog’s comments using the .Net WCF syndication API.
I’m using the XmlRPC.Net library to read all the blog posts but it doesn’t read the post comments.
Since the blogs platform has an RSS for all the comments then I can use the .Net WCF syndication API to read them and add them to my blog post structures.
What is The WCF Syndication Library?
Since .Net 3.5, WCF has a syndication library to help reading feeds of Atom and RSS. This library holds a simple API and supports Atom 1.0 and RSS 2.0 formats. In order to read or write a feed we can use the SyndicationFeed class that represent an instance of a feed or use the SyndicationItem class that represent an item in the feed. There are more classes to help parse feeds but the previous written classes are the major ones. The library can be found in the System.ServiceModel.Syndication namespace which is part of the System.ServiceModel namespace.
How to Use the Syndication API to Read a Comments RSS?
The first thing to do is to reference the System.ServiceModel library.
Then I have created a Comment struct to hold the comment data:
Now I can create a method that will return all the comments for a specific post id:
In the GetAllPostComments method I create the Url for the comments RSS. Then I create a SyndicationFeed object by using the Load static method in the SyndicationFeed class. This will enable me to read the feed data. Then I read all the syndication items and create the comments from the item data.
Using this method can look like:
Now I can add my comments to my posts.
Easy as that.
The .Net WCF syndication API makes it very easy to read RSS/Atom feeds. All we need to do is to load the syndication into a SyndicationFeed object and then use the syndication API to read the data we need. The .Net syndication library also supports the creation of a syndication feed using the same API.