Archive for December, 2008

Dec 16 2008

rss and atom api in .NET 3.5

Published by Raja Nadar under .net 3.5, syndication

i love the features which creep into an API. and the .NET framework classes grow richer and richer. my latest random activity has been to develop User controls (blog rolls, rss widgets) in .NET 3.5.

to think of it, one can easily write an RSS reader control, with inline commenting feature for blog posts, searching by category, authors etc. all that, by just using the Syndication API in .NET 3.5.

 

here are the classes that i am talking about:

  • SyndicationFeed: this is the top-level class representing the RSS or ATOM feed
  • SyndicationItem: this represents a blog post
  • SyndicationCategory: a category defined in a blog, applied to posts
  • SyndicationLink: a link associated with a blog post. (self, edit, comments etc)
  • SyndicationPerson: author of the blog post (name, email, profile Url etc)

 

without further ado, here is a quick snippet to get you started on the RSS/Atom APIs..

firstly, a reference needs to be added:

System.ServiceModel.Web

the namespaces required are:

using System.ServiceModel.Syndication;
using System.Xml;

Code Snippet:

string atomOrRssUrl = "http://rajanadar.blogspot.com/atom.xml";
 
SyndicationFeed rssOrAtomFeed = null;
 
using (XmlReader feedXmlReader = XmlReader.Create(atomOrRssUrl))
{
    // Top-level feed in (Atom 1.0) or Rss 2.0
    rssOrAtomFeed = SyndicationFeed.Load(feedXmlReader);
 
    // Read the blog posts in this blog.
    foreach (SyndicationItem blogItem in rssOrAtomFeed.Items)
    {
        // Important Blog Post attributes.
        blogItem.Id;
        blogItem.Title.Text;
        blogItem.PublishDate;
        blogItem.LastUpdatedTime;
        blogItem.Copyright;
 
        // Contents
        if (blogItem.Content is TextSyndicationContent)
        {
            // typically, this will be HTML.
            (blogItem.Content as TextSyndicationContent).Text;
        }
 
        // Categories of this blog post.
        foreach (SyndicationCategory category in blogItem.Categories)
        {
            category.Name;
            category.Scheme;
            category.Label;
        }
 
        // Get the Links associated with this blog post.
        // e.g. Comments (replies), self, alternate, edit links etc
        foreach (SyndicationLink link in blogItem.Links)
        {
            link.Title;
            link.Uri;
            link.RelationshipType;
        }
 
        // Get the Authors associated with the blog post.
        foreach (SyndicationPerson author in blogItem.Authors)
        {
            author.Name;  // Raja Nadar
            author.Email; // noreply@blogger.com
            author.Uri;   // Profile Url for blogger
        }
    }
}

obviously, the code won’t compile :), not because i wrote it, but that i have just highlighted the relevant fields I used. not assigned or used them anywhere..

 

so explore the Syndication APIs and code happily ever after.

No responses yet