Saturday, August 4, 2012

Feed Reader Using C#


Feed Reader Using C#

.NET framework provides class called SyndicationFeed which is in defined in the System.ServiceModel.Syndication namespace. . The class is found in the System.ServiceModel.Web.dll assembly when using the .NET framework version 3.5 and in System.ServiceModel.dll for .NET 4.0 For both framework versions.

Add DLL to you application according to .NET Framework version.

Add Following declarative to your application

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

In order to read the feed, the XmlTextReader should be created and linked to the URL of the RSS or Atom feed. This can be achieved using a constructor that accepts the URL as a string parameter. The reader is then passed to the static Load method of the SyndicationFeed class. 

The following sample code loads the RSS feed for this web site:

XmlTextReader reader = new XmlTextReader( "feedurl");
SyndicationFeed feed = SyndicationFeed.Load(reader);

Once downloaded, you can extract the required information from the generated object. For example, the following outputs the title of the feed.

Console.WriteLine(feed.Title.Text);

Use below link to find about class details

Using below code you can read each feed item.

foreach (SyndicationItem item in feed.Items)
{
    Console.ForegroundColor = ConsoleColor.White;
    Console.WriteLine(item.Title.Text);
    Console.ForegroundColor = ConsoleColor.Gray;
    Console.WriteLine(item.Summary.Text);
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine(item.Links.First().Uri);
    Console.WriteLine();
}


No comments:

Post a Comment