RSS / Atom Aggregator WebPart - Syndication using WCF

Preface: In my previous post, I wrote about the RSS Aggregator Webpart. That version of code used XMLDom model. However, in .NET 3.5 we have syndication classes available and you can see how reading RSS / Atom feeds have become so easy. This supports Atom 1.0 and RSS 2.0 versions.

Since the process is same, you can pass multiple feed url’s and make it aggregator too.

Assembly Reference: System.ServiceModel.Syndication

Solution:

public void GetSyndication(string url)
{
SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create(url));
if (null != feed)
{
foreach (SyndicationItem item in feed.Items)
{
//Link to the Post - item.Links[0].Uri.AbsoluteUri.ToString()
//Title of the Post - item.Title.Text
//Description - item.Summary.Text
}
}
}

Most of the time, you will end up querying either of Atom / RSS formats and you never know which one is the input. So, I kind of had a check point to validate the input formats. If one of the conversion fails, its obviously the other. Well here is the full code.


Your entry point would be GetSyndicationFeed Method if you dont know what's the incoming feed. However, if you know the feed type then you can directly call either the GetRSSFeedString method or GetAtomFeedString method directly.

public string GetSyndicationFeed(string url, string type)
{
if (string.IsNullOrEmpty(url)) return string.Empty;

string ret = string.Empty;
switch (type.ToLower())
{
case "rss":
ret= GetRSSFeedString(url);
break;
case "atom":
ret= GetAtomFeedString(url);
break;
case "": // Acts as a validator
try {
Rss20FeedFormatter rss = new Rss20FeedFormatter();
rss.ReadFrom(XmlReader.Create(url));
ret= GetRSSFeedString(url);
}
catch (XmlException exml){
ret= GetAtomFeedString(url);
}
break;
}
return ret;
}

public string GetRSSFeedString(string url)
{
StringBuilder sb = new StringBuilder();
Rss20FeedFormatter rss = new Rss20FeedFormatter();

try
{
rss.ReadFrom(XmlReader.Create(url));
}
catch (XmlException exml)
{
// Log it.
}

if (null == rss.Feed) return string.Empty;

foreach (SyndicationItem item in rss.Feed.Items)
{
// item.Links[0].Uri.AbsoluteUri.ToString() // - URL link to the post
// item.Summary.Text.ToString() // - Description or Summary of the post
// item.Title.Text // - Title of the post
}

return sb.ToString();
}

public string GetAtomFeedString(string url)
{
StringBuilder sb = new StringBuilder();
Atom10FeedFormatter atom = new Atom10FeedFormatter();

try
{
atom.ReadFrom(XmlReader.Create(url));
}
catch (XmlException exml)
{
// Log it.
}

if (null == atom.Feed) return string.Empty;

foreach (SyndicationItem item in atom.Feed.Items)
{
// item.Links[4].Uri.AbsoluteUri.ToString() // - URL link to the post
// ((System.ServiceModel.Syndication.TextSyndicationContent)(item.Content)).Text // - Description or Summary of the post
// item.Title.Text // - Title of the post
}

return sb.ToString();
}

Technorati Tags: ,,,,

Comments

Sandeep said…
did you figure out what was the issue with WebPage running the same code