In this tutorial, we are going to discuss how to fetch RSS feed and have them displayed on your site. This is when you need to display news or such content. This article will help you fetch and display RSS feeds on your site. We are doing so using C# and ASP.NET. So, let get started.

ASP.NET and RSS feed

  • After you have launched Visual Studio, click on File at the top left corner.
  • Select New > Project.
  • In the New Project window, select Web from the options on the left.
  • Select ASP.NET Web Forms Application.
  • Set the location from the options at the bottom where you’d like to store the application you are creating.
  • Give a suitable name as well. The project here has been named ‘RSSFeedReader‘.
  • The next step would be to add a class.

Adding a Class

  • Right-click on the project name in Solutions Explorer.
  • From the options that show, select Add > New Item.
  • In the Add New Item windows that opens, select Code from the options on the left.
  • Select Class from the options in the middle.
  • Give a Class name, like in this case, RSS.
  • Click OK. In the RSS.cs page, enter the following code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
 namespace RSSFeedReader
    {
        public class RSS
        {
            public string Title { get; set; }
            public string Link { get; set; }
            public string PublishDate { get; set; }
            public string Description { get; set; }
        }
    }

You will next need a page to display the RSS feed.

  • In Solution Explorer, click on the Default.aspx page under your project.
  • Enter the following code on the page.
<h3>Read RSS Feed from "XYZ"</h3>
/* Where XYZ refers to the publication from where you wish to fetch the RSS feed from */
    <div style="max-height:350px; overflow:auto">
        <asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <table width="100%" border="0" cellpadding="0" cellspacing="5">
                            <tr>
                                <td>
                                    <h3 style="color:#3E7CFF"><%#Eval("Title") %></h3>
                                </td>
                                <td width="200px">
                                    <%#Eval("PublishDate") %>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <hr />
                                    <%#Eval("Description") %>
                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td align="right">
                                    <a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>

Now, you will have to write the code to fetch RSS feed.

  • For that, Right-Click on an empty portion on the Default.aspx page and select View Code.
  • In the Default.aspx.cs page that opens, enter the following code.
  private void PopulateRssFeed()
    {
        string RssFeedUrl = "The URL of the site from where you wish to fetch the RSS feed from";
        List<Feeds> feeds = new List<Feeds>();
        try
        {
            XDocument xDoc = new XDocument();
            xDoc = XDocument.Load(RssFeedUrl);
            var items = (from x in xDoc.Descendants("item")
                         select new
                         {
                             title = x.Element("title").Value,
                             link = x.Element("link").Value,
                             pubDate = x.Element("pubDate").Value,
                             description = x.Element("description").Value
                         });
            if (items != null)
            {
                foreach (var i in items)
                {
                    Feeds f = new Feeds
                    {
                        Title = i.title,
                        Link = i.link,
                        PublishDate = i.pubDate,
                        Description = i.description
                    };

                    feeds.Add(f);
                }
            }

            gvRss.DataSource = feeds;
            gvRss.DataBind();
        }
        catch (Exception ex)
        {
            throw;
        }
    }

That’s it.

Run the application. The page just created will fetch RSS feed from the URL provided and will display the same as well.

Related Articles

Last modified: December 15, 2019

Comments

Write a Reply or Comment

Your email address will not be published.