CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
RSS Reader in ASP.Net 2.0

By Satheesh babu
Posted On Jun 07,2008
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category: ASP.Net
Print this article.

RSS Reader in ASP.Net 2.0

 

RSS feed is an xml formatted document that gives us the capability to read frequently updated contents on our websites without visiting our site. It is one of the web 2.0 features which allow users to read the relevant information’s available on the internet. All we need to do is to get the RSS feed URL from different sites of our interest and subscribe it on our RSS reader. This article will help us to construct our own RSS reader in ASP.Net.

Before moving into actual implementation of RSS reader we will understand a sample Rss document.

 

For example, CodeDigest Rss feed will look like,

<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0">

  <channel>

  <title>CodeDigest.com Latest Articles</title>

  <link>http://www.codedigest.com</link>

  <description>Latest articles hosted on CodeDigest.com.</description>

  <item>

  <title>Useful Datagrid Tips</title>

  <description>There are some frequent queries that are being asked in most of the forums and UG’s regarding datagrid. I have compiled a list of useful tips from my previous posts and posted it here.</description>

  <link>http://www.codedigest.com/Articles/ASPNET/77_Useful_Datagrid_Tips.aspx</link>

  <pubDate>4/29/2008 9:47:12 AM</pubDate>

  </item>

</channel>

  </rss>

 

As we can see the above XML the whole contents is packed inside <rss> tag. The actual information about the posts is kept inside <item> tag which in turn is packed inside a <channel> tag. Read the history(http://en.wikipedia.org/wiki/RSS_(file_format)) and specification of RSS here(http://blogs.law.harvard.edu/tech/rss )

 

Binding RSS feed to DataList

This can be done using DataSet object ReadXml() method XmlTextReader Object.

XmlTextReader reader = null;

        try

        {

            reader = new XmlTextReader("http://www.codedigest.com/Articles/Rss.ashx");

            DataSet ds = new DataSet();

            ds.ReadXml(reader);

            dlRSS.DataSource = ds.Tables["item"];

            dlRSS.DataBind();

        }

        catch (Exception ex)

        {

            lblMessage.Text = ex.Message;

        }

        finally

        {

            reader.Close();

        }




From the sample RSS XML given above, we can find the actual posts/articles in the table with the name “item” because they are packed inside the <item> tag.

DataList

    <asp:DataList ID="dlRSS" runat="server" Width="100%">

        <ItemTemplate>      

                    <div class="RSSTitle"><asp:HyperLink ID="TitleLink" runat="server" Text='<%# Eval("title") %>' NavigateUrl='<%# Eval("link") %>'/></div>

                    <div class="RSSSubtitle"><asp:Label ID="SubtitleLabel" runat="server" Text='<%# Eval("description") %>' /></div>

                    <div class="RSSInfo">

                        posted on <asp:Label ID="DateRSSedLabel" runat="server" Text='<%# Eval("pubDate", "{0:d} @ {0:t}") %>' />

                    </div>         

        </ItemTemplate>

    </asp:DataList>

 

The output will be similar to below,

 

Since the above code connects to an external site to download the contents the best practice is to cache it instead of downloading it again and again. To do this we can put the datalist inside a user control and include an output cache directive like below.

 

<%@ OutputCache Duration="3600" VaryByParam="None" %>

 

Read the article posted by Suprotim on consuming RSS feed using XmlDataSource controls here.

Consuming RSS Feed

 

Conclusion

Thus we can use RSS feeds to get the information without visiting the actual website. Also, we can use the RSS feed to get the latest news, technical articles etc from a different website and post it on our websites. Visit our homepage (codedigest.com) where we have used the RSS feed of www.asp.net and got all the latest articles posted on the site. I will discuss the implementation of construction of RSS feeds for our sites in my next article.

Happy Coding!!

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments