Using .Net WCF Syndication API to Read a Comments Feed

  • submit to reddit

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior .Net Consultant and Architect at Sela Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft. You can find his Blog here: http://www.gilfink.net Gil is a DZone MVB and is not an employee of DZone and has posted 92 posts at DZone. You can read more from them at their website. View Full User Profile

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.

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.

References
0
Average: 5 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)