Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

LINQ to RSS

4.45/5 (8 votes)
1 Feb 2011CPOL2 min read 25K  
Language Integrated Query in RSS

Introduction

RSS stands for Really Simple Syndication. RSS is a sort of web feed which allows us to publish any kind of frequent data such as news, blogs, ... etc.
LINQ is a cool technology for querying because it's data source independent, so from that point I started developing LINQ to RSS, which is a simple library that simplifies the querying with RSS documents.

Background

RSS is an XML-based document that contains the feeds for a specific channel, it has .rss or .xml extension.

XML
<rss version="2.0">

  <channel>

    <title>News Headlines</title>
    <link>http://www.myWebsite.com/</link>
    <description>News Website.</description>
    <lastBuildDate>Mon, 06 Sep 2010 00:01:00</lastBuildDate>
    <pubDate>2009-01-01</pubDate>

    <item>
      <title>Title1</title>
      <link>http://www.myWebsite.com/news/4371</link>
      <description>
        some description goes here.
      </description>
      <guid>4f71539d-0341-4545-b2b8-90cd13562d4e</guid>
      <pubDate>2010-01-23</pubDate>
    </item>
    <item>
      <title>Space Exploration</title>
      <link>http://www.myWebsite.com/news/4372</link>
      <description>
        some description goes here.
      </description>
      <guid>cd13590c-45c4-ae76-abbb1-3e3effffa12e</guid>
      <pubDate>2010-01-27</pubDate>
    </item>
  </channel>
</rss>

From the above file, we can notice that each RSS file contains channel tag which contains a descriptive information about the current channel that provides the feeds, underneath item tag which represents the feeds themselves. Each item contains a descriptive information about a specific item such as title, link and so forth.

There's no magic in LINQ .. just whenever you have any kind of data source XML, database, registry... etc. If you represent it into objects, it's over the LINQ which will take care about the other stuff, originally there are Lambda Expressions, Extension Methods and so forth that makes LINQ easier to query those objects.

Using the Code

Behind the scenes, the important thing in my code is the RSSDataContext class, which is a very simple class that contains a descriptive about RSS document: Channel, Feeds.

VB.NET
Public Class RssDataContext
    Public Sub New(ByVal Path As String)
        Dim doc As XDocument = XDocument.Load(Path)
        Feeds = From item In doc.Element("rss").Element("channel").Elements("item")
                Select New Feed With {.Title = item.Element("title").Value, _
		.Link = item.Element("link").Value, _
		.Description = item.Element("description").Value, _
		.PublicationDate = item.Element("pubDate").Value, _
		.GUID = Guid.Parse(item.Element("guid").Value)}

        Channel = New Channel() With {.Title = doc...<title>.Value, _
		.Link = doc...<link>.Value, _
		.Description = doc...<description>.Value, _
		.PublicationDate = doc...<pubDate>.Value, _
		.LastBuildDate = doc...<lastBuildDate>.Value}
    End Sub

    Public Property Feeds As IEnumerable(Of Feed)

    Public Property Channel As Channel
End Class		

Feed class is nothing but a piece pf code that represents each feed in the RSS document:

VB.NET
Public Class Feed
    Property Title As String
    Property Link As String
    Property Description As String
    Property PublicationDate As DateTime
    Property GUID As Guid
End Class

as well as channel class:

VB.NET
Public Class Channel
    Property Title As String
    Property Link As String
    Property Description As String
    Property PublicationDate As DateTime
    Property LastBuildDate As DateTime
End Class

Now we can use it like this:

VB.NET
Dim db As New RssDataContext("rss.xml")
        Dim feeds = From f In db.Feeds
                    Select f

        Console.WriteLine(db.Channel.Title)
        Console.WriteLine(db.Channel.Link)
        Console.WriteLine(db.Channel.Description)
        Console.WriteLine(db.Channel.PublicationDate)
        Console.WriteLine(db.Channel.LastBuildDate)
        Console.WriteLine()
        For Each f As Feed In feeds
            Console.WriteLine(f.Title)
        Next

I think it's cool that whenever someone wants to query RSS document with strong type instead of dealing with XML, there's no need to spend a lot of time learning XML programming when you want any information from RSS. Just try my code and I hope you enjoy it.

Points of Interest

  • Developing RSS Aggregator (Reader)
  • Display a bunch of the web feeds inside your web application (RSS Feed Module)

History

  • LinqToRss version 1.0 which contains the basic functionality to query and RSS document

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)