DSMX Data Relation to consume RSS 2.0 feeds 1/2

A Data relation that consumes an RSS 2.0 XML data stream is pretty straight forward to implement. All it needs is basically an URL pointing to the RSS Feed provider and a .net WebClient object that downloads the XML from the server and deserializes it into a data relation table.

But let’s start by implementing the data relation. To do so, create a new class library project in Visual Studio, doesn’t matter if C# or VB.NET, I’m using vb.

Add a reference to the DataRelationInterfaces.dll and create a new class, named RssFeedRelation, which implements IDataRelation.

GetMetaData

The GetMetaData method is called by DSMX to get metadata information from the relation. This is where we add two tables, one that will provide basic information like link, description and name of the RSS feed. A second table that contains a list of all available RSS feed items.

We also want to add to parameters, one for the server URL and another for the provided encoding. Because in most cases the encoding is utf-8, we set this as the default. I also added a default for the Feed URL as well.

Public Sub GetMetaData(ByVal accountID As Integer, ByVal Language As String, ByVal MetaData As IhtDataProviderMetaData) Implements IDataRelation.GetMetaData
     MetaData.AddTable(RssFeedPropertiesTable)
     MetaData.AddTable(RssFeedTable)
     MetaData.AddParameter("Encoding", "RSS Feed XML content encoding", "utf-8", False)
     MetaData.AddParameter("RssFeedUrl", "Url pointing to the RSS feed XML.", "http://support.directsmile.de/support/rss.aspx", False)
End Sub

LoadData

Implementing the LoadData method that is actually called by DSMX to get the feed data is also no black magic at all.

 Public Sub LoadData(ByVal table As IhtDataTable) Implements IDataRelation.LoadData
        Dim url As String = table.GetParameterValue("RssFeedUrl")
        Dim enc As String = table.GetParameterValue("Encoding")
        If table.Query.TableName = RssFeedTable Then
            AddFeedItemTableFields(table)
            Dim itms = LoadFeedItems(url, enc)
            If Not itms Is Nothing AndAlso itms.Count > 0 Then
                Dim pagedResult As IEnumerable(Of RssItem) = GetPagedResultSet(table, itms)
                AddFeedItems(table, pagedResult)
            End If
        ElseIf table.Query.TableName = RssFeedPropertiesTable Then
            AddFeedPropertiesTableFields(table)
            Dim itms = LoadFeedItems(url, enc)
            If Not itms Is Nothing AndAlso itms.Count > 0 Then
                AddFeedProperties(table, itms)
            End If
   Else
            Throw New ArgumentException("No valid table name found.")
End If

That method does two things, first it checks what table was requested by DSMX. It contacts the Feed providing server and downloads the feed XML to return either the list of items or just the feed header.

LoadItems

The LoadItems method instanciates a WebClient and downloads the XML.

Public Function LoadFeedItems(url As String, enc As String) As IEnumerable(Of RssItem)
        If String.IsNullOrEmpty(url) Then
            Throw New ArgumentException("RSS feed URL must have a value.")
        End If
        Dim wc As New WebClient()
        wc.Encoding = Encoding.GetEncoding(enc)
        Dim result = wc.DownloadString(New Uri(url))
        If String.IsNullOrEmpty(result) Then
            Throw New ArgumentException("RSS feed URL returned no valid RSS data [" & url & "]")
        End If
        Dim rssFeedItems = New RssItems
        rssFeedItems.Deserialize(result)
        Return rssFeedItems
End Function

If the download was successful we can deserialize the XML data into a CLR object. Very convenient using XML literals in VB.NET.

Public Sub Deserialize(xml As String)
        If String.IsNullOrEmpty(xml) Then
            Throw New ArgumentException("Feed xml contains to data.")
        End If
        Dim doc As XDocument = XDocument.Parse(xml)
        If doc Is Nothing Then
            Throw New Exception("Failed to parse rss feed xml.")
        End If
        Title = doc...<channel>.<title>.Value
        Link = doc...<channel>.<link>.Value
        Description = doc...<channel>.<description>.Value
        For Each elem In From element In doc...<channel>.<item>
            If elem.Name = "item" Then
                Add(New RssItem With {.Author = elem.<author>.Value,
                                      .Description = elem.<description>.Value,
                                      .Link = elem.<link>.Value,
                                      .PubDate = TryConvertToDate(elem.<pubDate>.Value),
                                      .Title = elem.<title>.Value,
                                      .EnclosureUrl = elem.<enclosure>.@url,
                                      .EnclosureType = elem.<enclosure>.@type})
            End If
        Next
End Sub

Conclusion

Basically that’s all it needs to consume RSS feeds. In part 2 of this short series I will create a little mobile RSS feed reader to show you easy it is to integrate this data relation into a cross media server.

Have fun

Oliver

About Oliver Dehne
Father of two little boys, good boys. Sometimes I like coding, trying to be a good developer.

One Response to DSMX Data Relation to consume RSS 2.0 feeds 1/2

  1. Pingback: Designing a mobile RSS 2.0 feed reader app that uses a Cross Media Data Relation | Welcome to Olivers Blog

Leave a comment