RSS feed reader in ASP.NET can be created in different ways. Today I have written this tutorial to do this task as simple as I could. This tutorial can help newbies who get scared of complex code and confusing custom controls. I have used Visual Studio 2008 SP1 on Windows Vista SP1 and .NET 3.5. The output looks like this:

image

Here is how to do it:

Start Visual Studio and create a new project by going to File>New Project. In project types select Web and in Templates select ASP.NET Web Application. Name this Website as “FeedReader” and click ok.

image

First let’s create some UI for the web page. double click default.aspx in the solution explorer. And click Design button.

image

Insert a TextBox, Button and a DataList to creating the following UI using tools in the Toolbox. For this this tutorial, you need to set some specific properties: set ID property of TextBox as “TB_url”, Button as “BT_GetFeeds” and DataList as “DL_Feeds”

image

Now double click the BT_GetFeeds Button Default.aspx.cs will open. You will see this screen

image

A new function named BT_GetFeeds_Click Will be created. This method/function will be called when user clicks the Get Feeds button.

You will have to import/use the following namespaces here, add the missing ones:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

Now add the following code in BT_GetFeeds_Click method

protected void BT_GetFeeds_Click(object sender, EventArgs e)
        {
            string rssUrl = TB_url.Text;

            XDocument rssFeed = XDocument.Load(rssUrl);

            var posts = from item in rssFeed.Descendants("item")

                        select new
                        {
                            Title = item.Element("title").Value,
                            Url = item.Element("link").Value,

                        };

            DL_Feeds.DataSource = posts;
            DL_Feeds.DataBind();
        }

A little explanation of code: I am using using LINQ to XML to fetch feeds. First RSS Feeds are loaded in an XDocument. XDocument contains an XML file. If you have some idea of how RSS Feeds are structured, then you can understand that how the code is working. In short there is something like this in XML file:

<item>

<title>This is a Title</title>

<link>https://somelink.com</link>

</item>

Of course the structure is more complex but I am trying to create an image in your mind. if you compare the above XML code to the C# code above. You will get a little idea how feeds are being fetched. To learn more about how RSS feeds are structured see this or search around.

Now switch back to design view of default.aspx. Select the DataList and click the small arrow button to open DataList Tasks

image

Then click on Edit Templates to open Item Template of DataList

image Insert a new HyperLink inside ItemTemplate and click on the small arrow button to open HyperLink Tasks

image

Click on Edit DataBindings… and HyperLink1 DataBindings window will open. In Bindable properties select Text and in Custom Binding set this Code Expression: Eval("Title")

And again in Bindable properties select NavigateUrl and in Custom Binding set this Code Expression:Eval("Url")

Click ok

image

Optional: To make the appearance look a little attractive, I have changed the font of hyperlink and added some grid lines. To do this select the HyperLink and in properties, set the GridLines to horizontal and change the font to Verdana.

Now Press F5 to debug and if Debugging not enabled window appears, then click ok.

…And now! enjoy 😀

image

Note: If you are confused about RSS Feed URL of blogs, then normally it is https://<blog-address>/feed like programmerfish’s feed URL is https://www.programmerfish.com/feed

Download the solution: FeedReader.zip