Home > Tutorials > .NET > Reeding RSS Feeds

Reading RSS Feeds

This tutorial will show how to load RSS feeds using the SyndicationFeed class. The first thing you want to do is add a reference to System.ServiceModel.Web as it it required for the class.

Here is the entire program.

  • using System;
  • using System.Xml;
  • using System.ServiceModel.Syndication;
  •  
  • namespace RSSTutorial
  • {
  • class Program
  • {
  • static void Main(string[] args)
  • {
  • string feedurl = "http://swiftoutlaw.net/Feed.ashx";
  • XmlReader reader = XmlReader.Create(feedurl);
  • foreach(SyndicationItem entry in feed.Items)
  • {
  • Console.WriteLine(entry.Title.Text);
  • Console.WriteLine(entry.PublishDate.ToString());
  • Console.WriteLine(entry.Summary.Text);
  • Console.WriteLine(entry.Links[0].Uri.OriginalString);
  • Console.WriteLine("");
  • }
  • }
  • }
  • }

First, the string feedurl is the URL to the feed you wish to read. Next, we create an XmlReader that will download(if needed) and load the feed into memory. After that, we create an object of the SyndicationFeed class that will be used to access the different elements of the feed. Then we use a foreach statement to write some of the properties of each item in the feed to the console.

Note this line:

  • Console.WriteLine(entry.Links[0].Uri.OriginalString);

Feeds can have mutliple links for a single entry. The line above simply uses the first one.

If you use a firewall, make sure you allow the application access to the internet, or else you'll get an error.