C# RSS Feed Fetcher - Display RSS Feed with 2 lines of Code
There is a new set of classes under the System.ServiceModel.Syndication Namespace. These classes are designed to work with RSS and ATOM Feeds, whether you want to generate or consume Feeds in ASP.NET or WinForms applications.
However, if you are looking to simply fetch and display RSS feed from any source, it can be done with just two lines of Code. This uses Linq, so ASP.NET 3.5+ is needed for it to work.
//www.haiders.net | Jan 2010
//C# Example: Fetch and Shape RSS Feed
string rssUri = "http://some.feed.uri.xml";
var doc = System.Xml.Linq.XDocument.Load(rssUri);
var rssFeed = from el in doc.Elements("rss").Elements("channel").Elements("item")
select new { Title = el.Element("title").Value, Link = el.Element("link").Value,
Description = el.Element("description").Value };
//The data is ready, assuming we have a ListView to display the Feed named lvFeed
//Lets bind the Feed to the ListView
lvFeed.DataSource = rssFeed;
lvFeed.DataBind();
//Thats all!
In order to display the Feed on an ASP.NET Page, you may use a ListView Control with a simple Template:
The sample ListView here only displays a list of Post Titles with Links, but you can modify it to show the Description and other attributes available in the Feed.
You can see a live example of this on the Home Page of www.dojolocator.com, look at the "What's New?" section on the right panel. Another Page showing the Same RSS Feed in detail: www.dojolocator.com/news/
Posted on February 4, 2010 07:09 by
Haider