Monday, March 8, 2010

Technical: Fetching blog feeds in .NET

Hi Friends,

I would like to share how we can fetch the blog posts in .NET 3.5.

.NET Framework 3.5 had given us a dll: System.ServiceModel.Syndication. This dll gives a class SyndicationFeed in which we can extract the blog feeds.
We can read the blogpost in XmlReader.

Please find below a simple code snippet in C# to fetch the blog item & displaying it on console.

// Read the blog from the blog URL.
XmlReader reader = XmlReader.Create("http://aashita28.blogspot.com/feeds/posts/default");

// Create an object of SyndicationFeed class.
SyndicationFeed feed = SyndicationFeed.Load(reader);

// Access the items of feed object & display its properties.
foreach (var item in feed.Items)
{
Console.WriteLine("Title: " + item.Title.Text);
Console.WriteLine("Link :" + item.Links[0].Uri.ToString());
Console.WriteLine("Description :" + item.Summary.Text);
Console.WriteLine("PublishDate :" + item.PublishDate.LocalDateTime.ToLongDateString());
}

Using this class, we can display the blog item links on a webpage.

Thank you..

Happy Coding..!! :-)

No comments:

Post a Comment