Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » ASP.NET and Web » Add an RSS Feed Through a Custom Control

Add an RSS Feed Through a Custom Control

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.

Author Rank :
Page Views : 6412
Downloads : 278
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
RssFeedCode.zip | RssFeedCode.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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 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 RSS feed property is empty and, if it is not, it evokes the web services Get HTML 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.  RSS Feed and RSS Feed Title.  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 try – catch 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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.