In this post, we will see how to read RSS Feed data & show it on our web site.
To start with, we need to know from where we are going to read RSS feed data. In this example, we will read RSS feed data from blogger.com site, where recent posts are available.
Let’s say there is a blog spot as http://xxxxxx.blogspot.com, then RSS feed data would be available at http://xxxxx.blogspot.com/rss.xml. So this is the source data for us to work with.
Now start with code part:
- First let's add
DataGrid
to our web page, with one template column as shown below:
<asp:DataGrid runat="server" ID="myPosts" AutoGenerateColumns="False">
<columns>
<asp:TemplateColumn HeaderText="My Posts">
<itemtemplate>
<a href="<%# DataBinder.Eval(Container.DataItem, " link=")%>">
<%# DataBinder.Eval(Container.DataItem, "title") %>
</a>
</itemtemplate>
</columns>
- In our page load, let's declare
XMLTextReader
object & this will take RSS FEED URL. In this example, I am using my blogspot URL:
XmlTextReader reader = new XmlTextReader("http://bgsuryablog.blogspot.com/rss.xml");
- We will declare
Dataset
:
DataSet ds = new DataSet();
- To read XML data & read into DS, let's use the below code:
ds.ReadXml(reader);
- As we have
dataset
object filled up with RSS feed data, let's assign Dataset
to datagrid
as below:
myPosts.DataSource = ds;
myPosts.DataBind();
Dataset
object would contain properties in the form of Link & Title, which we have used in our .aspx code i.e. step 1.
- Now execute the code & see the output.
Happy coding… hope this helps!