How to Read or consume RSS Feed in Asp.net C# VB.Net

In my previous article i have explained “How you can create RSS feed using Asp.net C# VB.Net“. Here in this article i will explain how you can create a generic application to read or consume RSS Feed from any website. Please note that i am not going to explain about style sheet to represent the feed. Do it in your own way. You can use XSLT or inject CSS styles from code. How to inject CSS programmatically – read my code comments carefully.

Open a project. Rename the Default.aspx page to RSSFeedReader.aspx. Now add a server side Table control within the content. We will add RSS Feed rows in this table. Please find the HTML markup code from below:

<table runat="server" id="tbl_Feed_Reader" cellpadding="0" cellspacing="0">
        </table>

For this example i will use my codedisplay.com web site RSS. Now write the below code under Page_Load event:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        //Loading My site RSS FEED, Change as per your need
        WebRequest MyRssRequest = WebRequest.Create("http://codedisplay.com/?feed=rss");
        WebResponse MyRssResponse = MyRssRequest.GetResponse();

        Stream MyRssStream = MyRssResponse.GetResponseStream();

        XmlDocument MyRssDocument = new XmlDocument();
        MyRssDocument.Load(MyRssStream);

        XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");

        string sTitle = "";
        string sLink = "";
        string sDescription = "";

        // Iterate/Loop through RSS Feed items
        for (int i = 0; i < MyRssList.Count; i++)
        {
            XmlNode MyRssDetail;

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
            if (MyRssDetail != null)
                sTitle = MyRssDetail.InnerText;
            else
                sTitle = "";

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
            if (MyRssDetail != null)
                sLink = MyRssDetail.InnerText;
            else
                sLink = "";

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("description");
            if (MyRssDetail != null)
                sDescription = MyRssDetail.InnerText;
            else
            {
                sDescription = "";
            }

            // Now generating HTML table rows and cells based on Title,Link & Description
            HtmlTableCell block = new HtmlTableCell();
            
            // You can style the Title From Here
            block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>" + sTitle + "</a></span>";
            HtmlTableRow row = new HtmlTableRow();
            row.Cells.Add(block);
            tbl_Feed_Reader.Rows.Add(row);
            HtmlTableCell block_description = new HtmlTableCell();
            
            //You can style the Description from here
            block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>";
            HtmlTableRow row2 = new HtmlTableRow();
            row2.Cells.Add(block_description);
            tbl_Feed_Reader.Rows.Add(row2);
        }
    }

Note: Dont forget to add “using System.Net;”,”using System.Xml;”,”using System.IO;” namespaces in your application.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        'Loading My site RSS FEED, Change as per your need
        Dim MyRssRequest As WebRequest = WebRequest.Create("http://codedisplay.com/?feed=rss")
        Dim MyRssResponse As WebResponse = MyRssRequest.GetResponse()

        Dim MyRssStream As Stream = MyRssResponse.GetResponseStream()

        Dim MyRssDocument As XmlDocument = New XmlDocument()
        MyRssDocument.Load(MyRssStream)

        Dim MyRssList As XmlNodeList = MyRssDocument.SelectNodes("rss/channel/item")

        Dim sTitle As String = ""
        Dim sLink As String = ""
        Dim sDescription As String = ""

        'Iterate/Loop through RSS Feed items
        For i As Integer = 0 To MyRssList.Count - 1
            Dim MyRssDetail As XmlNode

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("title")
            If Not MyRssDetail Is Nothing Then
                sTitle = MyRssDetail.InnerText
            Else
                sTitle = ""
            End If

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("link")
            If Not MyRssDetail Is Nothing Then
                sLink = MyRssDetail.InnerText
            Else
                sLink = ""
            End If

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("description")
            If Not MyRssDetail Is Nothing Then
                sDescription = MyRssDetail.InnerText
            Else
                sDescription = ""
            End If

            'Now generating HTML table rows and cells based on Title,Link & Description
            Dim block As HtmlTableCell = New HtmlTableCell()

            'You can style the Title From Here
            block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>" + sTitle + "</a></span>"
            Dim row As HtmlTableRow = New HtmlTableRow()
            row.Cells.Add(block)
            tbl_Feed_Reader.Rows.Add(row)
            Dim block_description As HtmlTableCell = New HtmlTableCell()

            'You can style the Description from here
            block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>"
            Dim row2 As HtmlTableRow = New HtmlTableRow()
            row2.Cells.Add(block_description)
            tbl_Feed_Reader.Rows.Add(row2)
        Next
    End Sub

Note: Don’t forget to add “Imports System.Net”,”Imports System.Xml”,”Imports System.IO” namespaces in your project.

Now run the project. You will get a nice RSS Feed Reader like below:
Reading_RSS_FEED

Download Code Example C#        Download Code Example VB.Net

One thought on “How to Read or consume RSS Feed in Asp.net C# VB.Net

  • July 26, 2016 at 1:18 pm
    Permalink

    Thanks so much 🙂

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code