Click here to Skip to main content
Click here to Skip to main content

Add an RSS Feed Through a Custom Control

, 10 Nov 2006
Rate this:
Please Sign up or sign in to vote.
This article describes the construction of a very simple custom server control used to display the content returned from an RSS feed.

Introduction

This article describes the construction of a very simple custom server control used to display the content returned from an RSS feed. The control consumes a public web service to retrieve the current data from an RSS feed, and the control displays the content along with a user defined label. The public web service returns the feed content as straight HTML which greatly simplifies the process of displaying the data.

Figure 1: RSS Feed Custom Control in Use

Getting Started

The files included with this project include a web control library project and a demonstration web site. In order to get started, open the included zip file and install the two projects onto your file system. Open IIS and create a virtual directory for the web application. Open the solution into Visual Studio 2005 and make any changes necessary to bring both projects into the solution. Once properly configured, your Solution Explorer should show these projects, references, and files:

Figure 2: Solution Explorer with Web App and Control Library

In examining the solution, note that the RSS control library contains only a single control and that control is called “RssControl”. This project also includes a web reference that points to http://www.webservicex.net/WS/WSDetails.aspx?WSID=54&CATID=12; this public site supplies the web service web method called RssToHtml; this method retrieves the RSS feed content and converts it to HTML through its GetHTML web method.

The web application contains only a single web page (default.aspx) and includes a reference to the RSS DLL. It also contains a reference to my weather forecasting DLL which I added to the project as eye wash.

The web application serves as a container used to test the custom control. The page itself contains no additional code; there are four separate instances of the RSS Control on the page, along with a single instance of the weather forecasting control. The RSS controls are all pointing to different RSS feeds made available by the New York Times.

The Code: RSS Control

The RSS custom control is constructed to retrieve the information from the web service upon initialization; the information retrieved is the content from the specified RSS feed converted to HTML by the web service. Upon initialization, the HTML is retrieved and when the control is rendered, a label control is added and its text property is set to display the HTML. The control exposes two properties of interest, one is the RSS feed’s URL and the other is the title for the RSS control. The title and RSS feed properties may be set by the user.

In examining the code, note that only the default imports are included in the project. The class itself inherits from the WebControl class.

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<ToolboxData("<{0}:RssCC runat="server"></{0}:RssCC>")> _
Public Class RssCC
    Inherits WebControl

Following the class declaration, four member variables are declared within a declarations region; one for the web service and three string variables used to contain the RSS feed, the user defined RSS Feed Title, and one for the content returned by the web service.

    #Region "Declarations"

         Private mNews As net.webservicex.www.RSStoHTML
         Private mMessage As String
         Private mRssUrl As String
         Private mRssTitle As String

    #End Region

Next up is the methods region which contains the initialization event handler for the RSS control:

#Region "Methods"

    Private Sub RssCC_Init(ByVal sender As Object, _
            ByVal e As System.EventArgs) _
            Handles Me.Init

        If Not RssFeed Is String.Empty Then
            mNews = New net.webservicex.www.RSStoHTML
            mMessage = mNews.GetHTML(RssFeed)
        End If

    End Sub

#End Region

The initialization event handler checks to see if the RssFeed property is empty and, if it is not, it evokes the web services GetHTML web method to populate the message member variable with the HTML version of the information returned by the service.

The next region in the project is called “Properties” and it contains the two properties used by the control, RssFeed and RssFeedTitle. The properties update or read from the content of private member variables to manage their content. The RssFeed property is used to contain the path to the RSS feed while the RssFeedTitle property allows the user to key a title for the control.

#Region "Properties"

    <Category("RSS Feed")> _
    <Browsable(True)> _
    <Description("Enter the URL for the RSS Feed.")> _
    Public Property RssFeed() As String
        Get
            Return mRssUrl
        End Get
        Set(ByVal value As String)
            mRssUrl = value
        End Set
    End Property


    <Category("RSS Feed")> _
    <Browsable(True)> _
    <Description("Enter the title for the RSS Feed.")> _
    Public Property RssFeedTitle() As String
        Get
            Return mRssTitle
        End Get
        Set(ByVal value As String)
            mRssTitle = value
        End Set
    End Property

#End Region

The last detail to tend to is the rendering; here RenderContents is overridden and, within a TryCatch block, a set of nested Divs are created. Within the main Div there are two separate Divs, one to hold the user defined title for the feed, and one to display the HTML. In both instances, the content is rendered by a label control added to the Div. If the rendering operation fails, the control will display “RSS Feed Offline” to the user.

#Region "Rendering"

    Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)

        Try
            output.AddAttribute(HtmlTextWriterAttribute.Align, "center")
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "top")
            output.AddAttribute(HtmlTextWriterAttribute.Width, "100%")
            output.RenderBeginTag(HtmlTextWriterTag.Div)

            output.AddAttribute(HtmlTextWriterAttribute.Align, "left")
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "top")
            output.AddAttribute(HtmlTextWriterAttribute.Width, "50%")
            output.RenderBeginTag(HtmlTextWriterTag.Div)
            Dim lbl As New Label
            lbl.ID = "lblRssTitle"
            lbl.Text = "<b>" & Me.RssFeedTitle.ToString() & "</b>"
            lbl.RenderControl(output)
            output.RenderEndTag()

            output.AddAttribute(HtmlTextWriterAttribute.Align, "left")
            output.AddAttribute(HtmlTextWriterAttribute.Valign, "top")
            output.AddAttribute(HtmlTextWriterAttribute.Width, "50%")
            output.RenderBeginTag(HtmlTextWriterTag.Div)
            Dim lbl2 As New Label
            lbl2.ID = "lblRssContent"
            lbl2.Text = mMessage.ToString()
            lbl2.RenderControl(output)
            output.RenderEndTag()

            output.RenderEndTag()

        Catch

            output.RenderBeginTag(HtmlTextWriterTag.Div)
            output.Write("RSS Feed Offline")
            output.RenderEndTag()

        End Try

    End Sub

#End Region

Summary

This project demonstrates a very easy way to display RSS feed content on a web page through the use of custom server controls and web services. The control will only display the current or last feed from the RSS feed’s site; it would be necessary to capture and store multiple feed messages over time in order to display a group of them from any given RSS feed using this service. The same web service web method could be used to accomplish this task by capturing and saving the messages to a database, these stored strings containing the HTML version of the feed content could be used to populate labels in a similar manner to that used in this demonstration.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
QuestionWhy not just use XSLT to render the rss? Pinmemberkevferron10-Nov-06 12:32 
AnswerRe: Why not just use XSLT to render the rss? Pinmembersalysle10-Nov-06 12:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 Nov 2006
Article Copyright 2006 by salysle
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid