Using .Net WCF Syndication API to Read a Comments Feed

August 15, 2010

no comments

Using .Net WCF Syndication API to Read a Comments Feed

In the last few days I have started a migration process to my own domainWorking with Syndication Library 
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.

The Problem

I’m using the XmlRPC.Net library to read all the blog posts but it doesn’t read the post comments.

The Solution

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:

public struct Comment
{
  #region Properties
 
  public string title { get; set; }
  public string link { get; set; }
  public string author { get; set; }
  public DateTime pubDate { get; set; }
  public string description { get; set; }    
 
  #endregion
}

Now I can create a method that will return all the comments for a specific post id:

public static List<Comment> GetAllPostComments(int postid, string commentsRssPrefix)
{
  List<Comment> result = new List<Comment>();
  var commentsRSSURL = string.Concat(commentsRssPrefix, postid);
  SyndicationFeed feed = SyndicationFeed.Load(new XmlTextReader(commentsRSSURL));
  
  foreach (var item in feed.Items)
  {
    result.Add(new Comment
    {
      author = item.Authors.Count > 0 ? item.Authors[0].Name : string.Empty,
      description = item.Content != null ? item.Content.ToString() : string.Empty,
      link = item.Links.Count > 0 ? item.Links[0].Uri.ToString() : string.Empty,
      pubDate = item.PublishDate.UtcDateTime,
      title = item.Title.Text
    });        
  }
  return result;
}

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:

var comments = Migrator.GetAllPostComments(689817, "http://blogs.microsoft.co.il/blogs/gilf/rsscomments.aspx?PostID=");
foreach (var comment in comments)
{
  Console.WriteLine(comment.title);
}

Now I can add my comments to my posts.

Easy as that.

Summary

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.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>