Easy RSS in VB.NET

Posted by Phil Weber on March 5, 2003

I implemented an RSS feed for FTPOnline last night; .NET's XML serialization capabilities made it relatively painless.

First, I created this class to model the feed. (I actually cheated and used .NET's xsd.exe utility, first to create a schema based on my own RSS feed, then to generate a VB.NET class from that schema. All I had to do then was tweak the code to use a collection instead of an array for the items.)

Now all it takes to update the feed is to populate the RSS object and serialize it to XML:

    Dim rssFeed As New rss()
    Const BaseURL As String = "http://www.ftponline.com"

    With rssFeed.channel
        .title = "FTPOnline"
        .description = "Technical information for " & _
            "developers and IT professionals from " & _
            "the FTP family of publications and conferences."
        .link = BaseURL
    End With

    ' -- Query database for recent items

    ' Loop through DataReader, adding items to feed
    Do While drFeatures.Read
        Dim rssItem As New rssChannelItem()
        With rssItem
            .link = CStr(drFeatures!headerLink)
            ' Fully-qualify relative links
            If InStr(.link, "http://"= 0 Then
                .link = BaseURL & .link
            End If
            .title = CStr(drFeatures!headerText)
            .description = CStr(drFeatures!text)
            .pubDate = Format(drFeatures!dateCreated"R")
        End With
        rssFeed.channel.item.Add(rssItem)
    Loop
    drFeatures.Close()

    ' Serialize RSS object to file
    Dim xml As New XmlSerializer(GetType(rss))
    Dim strFile As New FileStream("d:\path\rss.xml"FileMode.Create)

    ' Empty namespaces collection eliminates 
    ' default namespace attributes from XML root
    Dim xmlns As New XmlSerializerNamespaces()
    xmlns.Add(String.EmptyString.Empty)

    xml.Serialize(strFile, rssFeed, xmlns)


Comments

Posted by G on November 21, 2003:

Thanks Phil! Just what I was looking for!


Posted by Rich on February 11, 2004:

Even easier:

DataSet ds = new DataSet(); ds.ReadXml(“http://www.startupskills.com//index.xml”,XmlReadMode.Auto);

‘Put it in a datagrid DataGrid1.DataSource=ds.Tables[2]; DataGrid1.DataBind();

This is the quickest and dirtiest way I know to get an RSS link, for example, for the latest article.

-Rich http://www.startupskills.com


Leave a comment