Follow us

Thursday, March 10, 2011

RSS Feed Reader / C# code to fetch RSS Feeds

Include the following Namespaces :

using System.Net;
using System.IO;
using System.Xml;
using System.Web.UI.HtmlControls;

Create a Table in Design Mode

<style type="text/css">
        .RssFeeds
        {
            width: 88%;
            font-family: verdana;
            font-size: x-small;
            border: 1px solid #000000;
            padding: 5px;
        }
    </style>
<body>
    <form id="form1" runat="server">
    <div>
        <h4>
            Technoscatter :: C# code to retrieve RSS Feeds</h4>
        <table>
            <tr>
                <td>
                    <table runat="server" id="tblNews" class="RssFeeds">
                    </table>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>

C# Code will fetch RSS feed and add them to the table 'tblNews'

public void RetrieveRssFeeds()
    {
        // URL from where feeds will be fetched
        string rssURL = "https://twitter.com/statuses/user_timeline/242292977.rss";

        string title = string.Empty;
        string link = string.Empty;
        string description = string.Empty;

        // No of feed items to be fetched
        int feedCount = 5;

        try
        {
            WebRequest myRequest = WebRequest.Create(rssURL);
            WebResponse myResponse = myRequest.GetResponse();
            Stream rssStream = myResponse.GetResponseStream();

            // Load a XML Document
            XmlDocument rssDoc = new XmlDocument();
            rssDoc.Load(rssStream);

            XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");

            // Loop through RSS Feed items
            ///
            // No. of Feeds to be fetched can be set by 'feedCount' which is by default set to value '5'
            ///
            for (int i = 0; i < feedCount; i++)
            {
                XmlNode rssDetail;

                rssDetail = rssItems.Item(i).SelectSingleNode("title");
                if (rssDetail != null)
                {
                    title = rssDetail.InnerText;
                }
                else
                {
                    title = string.Empty;
                }

                rssDetail = rssItems.Item(i).SelectSingleNode("link");
                if (rssDetail != null)
                {
                    link = rssDetail.InnerText;
                }
                else
                {
                    link = string.Empty;
                }

                rssDetail = rssItems.Item(i).SelectSingleNode("description");
                if (rssDetail != null)
                {
                    description = rssDetail.InnerText;
                }
                else
                {
                    description = string.Empty;
                }

                // Populate the HTML table rows and cells
                HtmlTableCell cell1 = new HtmlTableCell();
                cell1.InnerHtml = "<b><a href='" + link + "' target='new'>" + title + "</a></b>";
                HtmlTableRow row1 = new HtmlTableRow();
                row1.Cells.Add(cell1);
                tblNews.Rows.Add(row1);

                HtmlTableCell cell2 = new HtmlTableCell();
                cell2.InnerHtml = "<p align='justify'>" + description + "</p>";
                HtmlTableRow row2 = new HtmlTableRow();
                row2.Cells.Add(cell2);
                tblNews.Rows.Add(row2);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

Sr.No Download Size
1 C# RSS Feed Reader 4.04KB

Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

2 comments: on "RSS Feed Reader / C# code to fetch RSS Feeds"

Rishi Sharma said...

http://gstjaipur.blogspot.com

Borj said...

Hi is there a way it can read urls ending in .xml format?
like feed.yoursite/feeds.xml?

Post a Comment