Click here to Skip to main content
Click here to Skip to main content
Go to top

LINQ to RSS

, 1 Feb 2011
Rate this:
Please Sign up or sign in to vote.
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.

<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.

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:

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:

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:

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)

Share

About the Author

Hisham Abdullah Bin Ateya
Web Developer
Yemen Yemen
I'm interest with VB.NET and other Microsoft technologies such as LINQ, WPF, WCF, WF and much more


.:: Wish to become better and better ::.

Comments and Discussions

 
Generalnice one PinmemberPranay Rana1-Feb-11 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 2 Feb 2011
Article Copyright 2011 by Hisham Abdullah Bin Ateya
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid