Monday, 24 September 2012

.Net Tips , C# Tips : Process or Read the Content of an Atom or RSS Feed with C# Examples and VB.Net Examples

Click Here to Download

You can process the content of an Atom 1.0 or RSS 2.0 feed to extract or get details of the feed. .Net framework provide classes to parse the feed data.
We are using System.ServiceModel.Syndication.SyndicationFeedFormatter and System.ServiceModel.Syndication.SyndicationFeed classes.

The SyndicationFeedItem  and SyndicationFeed classes gives a generic abstraction of Atom 1.0 and RSS 2.0 feeds and feed items, and provide a common interface to simplify the processing of both feed types. The Rss20FeedFormatter class allows to create a SyndicationFeed object from an RSS 2.0 feed, and the Atom10FeedFormatter class provides support for Atom 1.0 feeds. Both classes are available in the System.ServiceModel.Syndication namespace.
We are using "ReadFrom" method and "Feed" property of these classes.

Here is example for this.
In this example we read the feed and display it's details.

C# Examples :
        Uri objFeedUrl = null;
        string strUrl = "";
        if (strUrl.Length == 0 || String.IsNullOrEmpty(strUrl) || !Uri.TryCreate(strUrl, UriKind.RelativeOrAbsolute, out objFeedUrl))
            Response.Write("Invalid feed.");

        // Create the web request.
        WebRequest objReqFeed = WebRequest.Create(objFeedUrl);

        // Get the data from the feed.
        WebResponse objResFeed = objReqFeed.GetResponse();

        SyndicationFeedFormatter objFeedFormatter = null;

        XElement feed = XElement.Load(objResFeed.GetResponseStream());

        // Check for the feed type
        if (feed.Name.LocalName == "rss")
            objFeedFormatter = new Rss20FeedFormatter();
        else if (feed.Name.LocalName == "feed")
            objFeedFormatter = new Atom10FeedFormatter();
            Response.Write("Unsupported feed type: " + feed.Name.LocalName);

        // Read the feed data into the formatter.

        // Display feed Details

        Response.Write("<b>Feed Title : </b>" + objFeedFormatter.Feed.Title.Text);
        Response.Write("<b>Feed Description : </b>" + objFeedFormatter.Feed.Description.Text);
        Response.Write("<b>Items in the Feed : </b>");
        foreach (var objItem in objFeedFormatter.Feed.Items)
            Response.Write("<b>Title : </b> " + objItem.Title.Text);
            if (objItem.Summary != null)
                Response.Write("<br/><b>Summary : </b>" + objItem.Summary.Text);

            Response.Write("<br/><b>Publish Date : </b>" + objItem.PublishDate);
        } Examples :
        Dim objFeedUrl As Uri = Nothing
        Dim strUrl As String = ""
        If strUrl.Length = 0 Or String.IsNullOrEmpty(strUrl) Or Not Uri.TryCreate(strUrl, UriKind.RelativeOrAbsolute, objFeedUrl) Then
            Response.Write("Invalid feed.")
        End If

        ' Create the web request.
        Dim objReqFeed As WebRequest = WebRequest.Create(objFeedUrl)

        ' Get the data from the feed.
        Dim objResFeed As WebResponse = objReqFeed.GetResponse()

        Dim objFeedFormatter As SyndicationFeedFormatter = Nothing

        Dim feed As XElement = XElement.Load(objResFeed.GetResponseStream())

        ' Check for the feed type
        If feed.Name.LocalName = "rss" Then
            objFeedFormatter = New Rss20FeedFormatter()
        ElseIf feed.Name.LocalName = "feed" Then
            objFeedFormatter = New Atom10FeedFormatter()
            Response.Write("Unsupported feed type: " & feed.Name.LocalName)
        End If

        ' Read the feed data into the formatter.

        ' Display feed Details

        Response.Write("<b>Feed Title : </b>" & objFeedFormatter.Feed.Title.Text)
        Response.Write("<b>Feed Description : </b>" & objFeedFormatter.Feed.Description.Text)
        Response.Write("<b>Items in the Feed : </b>")

        Dim objItem As SyndicationItem
        For Each objItem In objFeedFormatter.Feed.Items
            Response.Write("<b>Title : </b> " & objItem.Title.Text)
            If Not objItem.Summary Is Nothing Then
                Response.Write("<br/><b>Summary : </b>" & objItem.Summary.Text)
            End If

            Response.Write("<br/><b>Publish Date : </b>" & objItem.PublishDate.ToString())

Output : 
Read the Content of an Atom or RSS Feed output
(To view original image , click on image)

This is very useful .Net Tips.

For Beginning .Net articles. Click Here...

To learn more regarding XML. Click Here...

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


  1. Hey. Just wanted to say good example. But, it could be done alot easier using Linq.

    I can't post the code, but it's real simple via doing a simple XDocument.Load("FEEDURL"), selecting each item, and throwing it all into a strongly-typed list

    1. Hello,
      Yes you are right.
      But In my code I demonstrate validating the FEED URL and Step By Step process.

      Thanks for your valuable comments.

  2. Really nice information you had provided here. And i wanna appreciate within this.

    Digital Marketing Training in Chennai

    SEO Training in Chennai

  3. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.

    rpa training in chennai
    rpa training in bangalore | best rpa training in bangalore | rpa course in bangalore | rpa training institute in bangalore | rpa training in bangalore | rpa online training

  4. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
    Best Devops Training in pune


  5. You need to kill time, you need entertainment. Refer to our website. hope you get the most comfort.
    Thanks you for sharing!
    Deer Hunting Tips Camping Trips Guide DEER HUNTING TIPS travel touring tips


  6. I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again.

    travel trekking tips
    see the link Tent Camping 101 Exploring Smithriver

  7. Useful information, your blog is sharing unique information....Thanks for sharing!!!. oracle training in chennai

  8. Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging.After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts likethis.