Reading blog feed in Atom or RSS with LINQ

One of the most practical features a blog has its the possibility of requesting the latest posts from another webpage to read it without the need of accessing its current webpage. For doing this crazy idea blogs use XML technology, the REST protocol and one of the syndication standards like RSS, RSS2.0, Atom or RDF.

In this case I am going to use RSS2.0 to request the feed of my blog and I will manage it with Linq so that I will have a collection of my latest posts in a few lines. Let’s see the code:

    Private Sub readBlogRSS20()
        Dim feed As XDocument = XDocument.Load("http://www.rubencanton.com/blog/feed/")
        Dim sum = From item In feed.Element("rss").Element("channel").Elements("item") _
                  Select New BlogArticle(item.Element("title"), item.Element("link"), _
                                         item.Element("pubDate"), item.Element("description"))
    End Sub

I just load the xml in an XDocument (remember that the feed is an XML no matter which standard you use) and then select the nodes I want to use with Linq. For doing so I need to explore the XML tree, something easy having a look at it and then I build my object collection using an object I’ve declared before.

But after doing so I realized I could also access my feed using feedburner, so the reader will continue working despite I could change the blog url. That’s the code I needed to use:

    Private Sub readBlogFeedBurner()
        Dim fb As XNamespace = XNamespace.Get("http://rssnamespace.org/feedburner/ext/1.0")
        Dim feed As XDocument = XDocument.Load("http://feeds.feedburner.com/EntreCodigos")

        Dim sum = From item In feed.Element("rss").Element("channel").Elements("item") _
                  Select New BlogArticle(item.Element("title"), item.Element(fb + "origLink"), _
                                         item.Element("pubDate"), item.Element("description"))
    End Sub

Oh, have you noticed? Yes, we do have something different here. At first when loading RSS2.0 it was so simple because there was no namespace necessary for what we were doing but not on this case. The original link of the post its placed in a label called “feedburner:origLink”, and since I use permalinks I need to use that label instead of the label link which uses a feedburner url or the label guid which kills my permalinks.

To access that label I need to call the namespace (don’t try to just write “feedburner:origLink”, it does not work), but lucky us all the namespaces we need are placed in the first tag of the XML. So we load it and use it with the element needed. That’s all.

But now, since I am so curious, I wanted to do it using the Atom standard since wordpress lets you chose between the different standards when getting a feed. Would it be that easy?

    Private Sub readBlogAtom()
        Dim xmlns As XNamespace = XNamespace.Get("http://www.w3.org/2005/Atom")
        Dim feed As XDocument = XDocument.Load("http://www.rubencanton.com/blog/feed/atom/")

        Dim sum = From entry In feed.Element(xmlns + "feed").Elements(xmlns + "entry") _
                  Select New BlogArticle(entry.Element(xmlns + "title"), entry.Element(xmlns + "link").Attribute("href"), _
                                         entry.Element(xmlns + "published"), entry.Element(xmlns + "summary"))
    End Sub

Well it was, nearly. The most important difference is that in the Atom case you are forced to use the namespace including for those tags which are simple. So it makes the code a little more ugly but nothing important.

Finally, in case you want it, I show you the object I made for this purpose:

Public Class BlogArticle
    Private _Title As String = ""
    Private _DatePublished As Date = Nothing
    Private _URL As String = ""
    Private _Desc As String = ""

    Public Sub New()

    End Sub
    Public Sub New(ByVal pTitle As String, ByVal pUrl As String, ByVal pDatePublished As Date, ByVal pDesc As String)
        Title = pTitle
        URL = pUrl
        DatePublished = pDatePublished
        Description = pDesc
    End Sub

    Public Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property
    Public Property DatePublished() As Date
        Get
            Return _DatePublished
        End Get
        Set(ByVal value As Date)
            _DatePublished = value
        End Set
    End Property
    Public Property URL() As String
        Get
            Return _URL
        End Get
        Set(ByVal value As String)
            _URL = value
        End Set
    End Property
    Public Property Description() As String
        Get
            Return _Desc
        End Get
        Set(ByVal value As String)
            _Desc = value
        End Set
    End Property
End Class

One thought on “Reading blog feed in Atom or RSS with LINQ

Leave a Reply

Close Bitnami banner
Bitnami