How to consume an Rss feed using System.ServiceModel.Syndication Framework. (Part 1)

System.ServiceModel.Syndication provides several classes which are great for the manipulation of a feed resource. Using SyndicationFeed class

could helps us retrive all the required bits about particular feed resource and latest content, once the feed is loaded.

Below is sample program which demonstrates a syndicationFeed Object in action.

using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Xml;

namespace SyndicationFeedManager
{
    public class RssFeedManager
    {
        public void ParseRssFeed()
        {
            // Url To load Feed From
            string urlToLoadFeed = "";

            // Load Feed, Load Method requires and object of Xmlreader object
            SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create(urlToLoadFeed));           

            // get Title of Feed
            string feedTitle;
            if (feed.Title != null)
            {
                feedTitle = feed.Title.Text;
            }

            // get Short description of the feed
            string feedDescription;
            if (feed.Description != null)
            {
                feedDescription = feed.Description.Text;
            }

            // get the Icon url of the feed
            string feedIconUrl;
            if (feed.ImageUrl != null)
            {
                feedIconUrl = feed.ImageUrl.AbsoluteUri;
            }

            // get Image Url of the feed,  A snapshot
            string feedImageUrl;
            if (feed.ImageUrl != null)
            {
                feedImageUrl = feed.ImageUrl.AbsoluteUri;
            }

            // get copy right info of feed
            string feedCopyRights;
            if (feed.Copyright != null & !feed.Copyright.Text.Equals(""))
            {
                feedCopyRights = feed.Copyright.Text;
            }

            // get the categoried under whic feed is filed
            List feedCategories = new List();
            if (feed.Categories != null)
            {
                foreach (SyndicationCategory category in feed.Categories.ToList())
                {
                    if (category.Name != null)
                    {
                        feedCategories.Add(category.Name);
                    }
                }
            }

            // get the language feed has been published in
            string feedLanguage;
            if (feed.Language != null && feed.Language.Length > 1)
            {
                feedLanguage = feed.Language;
            }
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s