RSS stands for (Really Simple Syndication). Basically RSS feeds are xml files which are provided by many websites so you can view their contents on your own websites rather than browsing their site. Suppose you are a movie lover and you want to get the list of top 5 movies from 10 websites. One way will be to visit all 10 websites and see the top 5 list. This method though is used in general by lot of people but its quite tiring method. It will take you 10-15 minutes to browse all the websites and see the top 5 list of movies. One easy way will be if those movie websites provides RSS feeds to be used by the users. If they provide RSS feeds you can embed them in your page and now you don’t have to browse all the websites since the information is available on a single page. Hence, this saves you a time and a lot of browsing.
Most of the Blogs websites provide RSS feeds so you can embed your or someone’s else latest entries of blog on your website. In this article we will see how we can embed RSS feeds to our webform using Asp.net.
Making a simple user interface:
The application that we will build in this article will be really simple which extracts the Rss feeds from some website and display it in the datagrid. Below is the image of the User interface.
As you can see the user interface is pretty simple. It consists of a Label control which says “Rss Feeds”. It has a datagrid and a button with the text “Display RSS Feeds”. Let’s now dig into the code behind and see what’s happening behind the scenes.
Getting RSS Feeds from the website:
We have set up our interface and now our task is to find a website that provides Rss so we can display it on our webform. Luckily our own www.msdn.microsoft.com provides Rss feeds. This Rss feeds is about the latest news in the Microsoft industry. Here are the steps that you can use to get to the Rss feeds from the msdn website.
1) Go to www.msdn.microsoft.com
2) You will see a “RSS” link at the top as shown in the picture below:
3) Click on this link and you will see an XML File.
4) Just save the url of the Xml file that will be used later.
Displaying RSS feeds on your webform:
Finally, the time comes to code :). Yeah I know it always put a smile on your face right ?
First of all add the using System.Xml namespace at the top. Now in the button click place this code.
// Gets the xml from the Url using XmlTextReader XmlTextReader reader = new XmlTextReader("");
As you can see that we have used XmlTextReader to extract the Xml out of the url. The XmlTextReader has many overloads but we used the one which takes string url as a parameter. The parameter that we are sending is the url that you previously saved.
Now we got the Xml, next we put the xml into the dataset so we can bind it to the datagrid or else the datagrid will feel lonely :). So the next step is to create a DataSet object.
// creates a new instance of DataSet DataSet ds = new DataSet();
After creating the DataSet we need to fill the dataset with the Xml. DataSet has a ReadXml method that will allow us to ReadXml.
// Reads the xml into the dataset ds.ReadXml(reader);
Okay now the dataset, which is ds contains the xml. The final step will be to bind the dataset to the datagrid. Here is the line that binds the dataset to the datagrid.
// Assigns the datset to the datagrid myDataGrid.DataSource = ds; // Binds the datagrid myDataGrid.DataBind();
Okay looks good to me. Lets run the application and see what will happen. When you press the button the xml is extracted from the specified url and will be bind to the datagrid. You will see something like this:
Well, we can see that it did got something from the url and binding is done okay. So what’s wrong ? It seems like the data we were expecting is not correct. You can blame Microsoft for not providing us with the correct url but before that lets check our code again and see if we can fix it. The following code in button click event will fix the problem:
// Gets the xml from the Url using XmlTextReader XmlTextReader reader = new XmlTextReader("http://msdn.microsoft.com/rss.xml"); // creates a new instance of DataSet DataSet ds = new DataSet(); // Reads the xml into the dataset ds.ReadXml(reader); // Assigns the data table to the datagrid myDataGrid.DataSource = ds.Tables; // Binds the datagrid myDataGrid.DataBind();
As you see we replaced the line where we were binding dataset to the datagrid. Now we are binding Data Table. When the xml is put in the dataset it is put in layers. So first layer/level of xml nesting is placed in first row , 2nd layer in second row and so on. Each Rss file 3rd tag is the item tag which contains the information. Hence our 3rd tag is at he index ‘2’ since we start from ‘0’. You can try out different combinations with using different indexes and you will be returned with different types of tables.
Look at the code below in which I am referring to a different table and get the different result.
// Assigns the data table to the datagrid myDataGrid.DataSource = ds.Tables; // Binds the datagrid myDataGrid.DataBind();
As you see embedding Rss feeds on your own page is easy as baking a pie. The good side of using Rss will be that your website will be updated automatically and you won’t have to do anything. These days most of the websites are providing Rss feeds that you can use in your web pages.
Project files are also attached with this article. In the project I have added a textbox so you can enter the url of any Rss feed and when you press the button it will be extract the xml, which will be displayed on the web form in the datagrid.
Project Files: RssFeeds.Zip