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.