In this article, we will see how to read and filter the RSS Feed of codetechblog.blogspot.com using LINQ to XML. We will first read the RSS feed and populate the DropDownList with ‘Authors’ retrieved from the feed. We will then filter the feed results based on the Author selected in the DropDownList.
e will be reading and filtering the RSS feed of this site codetechblog.blogspot.com. The RSS feed can be obtained over here http://feeds.feedburner.com/codetechblog. Let us quickly jump to the solution.

Step 1: Create a new website (Open Visual Studio > File > New Website) called ‘FilterRSS.
Step 2: Drag and drop a GridView and a DropDownList control to the page.
Step 3: Once you visit the RSS Feed over here, right click on the page and View Source. You will find an XML file, since RSS is an XML file.
Let us now write a LINQ to XML query to read the Title, Link, Description and Author elements from our RSS Feed and filter the results. Add the following markup to the DropDownList and some template Columns to the GridView, to display content:

    <asp:DropDownList ID="DropDownList1" runat="server"
    <br /><br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <asp:TemplateField HeaderText="Title">
         <a href='<%# Eval("link") %>' target="_blank"><%# Eval("title") %></a>
        <asp:BoundField DataField="Author"
           HeaderText="Author" />
       <asp:BoundField DataField="description" HtmlEncode="false"
               HeaderText="Description" />

Step 4: Now create an ArticlesList class which will act as a container for the items read from the feed
Public Class ArticlesList
      Private privateTitle As String
      Public Property Title() As String
                  Return privateTitle
            End Get
            Set(ByVal value As String)
                  privateTitle = value
            End Set
      End Property
      Private privateLink As String
      Public Property Link() As String
                  Return privateLink
            End Get
            Set(ByVal value As String)
                  privateLink = value
            End Set
      End Property
      Private privateDescription As String
      Public Property Description() As String
                  Return privateDescription
            End Get
            Set(ByVal value As String)
                  privateDescription = value
            End Set
      End Property
      Private privateAuthor As String
      Public Property Author() As String
                  Return privateAuthor
            End Get
            Set(ByVal value As String)
                  privateAuthor = value
            End Set
      End Property
End Class

We are using Generics to return a strongly typed collection of IEnumerable items. This collection can now be passed to any other part of the application or stored in a session object to be used during postbacks.
The code will look similar to the following:

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Partial Public Class _Default
      Inherits System.Web.UI.Page
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            If (Not IsPostBack) Then
                  Dim xFeed As XElement = XElement.Load("http://feeds.feedburner.com/netCurryRecentArticles")
                  Dim items As IEnumerable(Of ArticlesList) = From item In xFeed.Elements("channel").Elements("item") _
                                                              Select New ArticlesList
                                    item.Element("description").Value, Author = item.Element("author").Value
                                    item.Element("link").Value, Description = item.Element("description").Value, Author
                                    item.Element("title").Value, Link = item.Element("link").Value, Description
                                    Title = item.Element("title").Value, Link
                  'store items in a session
                  Session("feed") = items
                  Dim authors = items.Select(Function(p) p.Author).Distinct()
                  DropDownList1.DataSource = authors
                  Dim selAuth As String = DropDownList1.SelectedValue
            End If
      End Sub
      Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim selAuth As String = DropDownList1.SelectedValue
      End Sub
      Protected Sub PopulateGrid(ByVal author As String)
            Dim items = CType(Session("feed"), IEnumerable(Of ArticlesList))
            If items IsNot Nothing Then
                  Dim filteredList = From p In items _
                                     Where p.Author = author _
                                     Select p
                  GridView1.DataSource = filteredList
            End If
      End Sub
End Class
In the code above, we load the Xml from the RSS feed using the XElement class. We then enumerate through all the Channel\Item elements and populate the items collection. This collection is stored in a Session object to be reused during postbacks.
Note: Observe the !IsPostBack which populates the collection only once, thereby saving the list from getting populated on every pageload.
We then apply a filter on this collection and select distinct authors and bind the result to a DropDownList. The selected author is passed to the PopulateGrid() method where the session object is casted back to IEnumerable. The list is then filtered based on the author passed and the results are bound to a GridView.
Similarly as and when the users selects a new author from the DropDownList, the list gets filtered and only the articles belonging to that author is displayed. Here are some screenshots:

I hope this article was useful and I thank you for viewing it


Post a Comment