display rss feed in asp.net page

Keywords | display rss feed asp.net c#, display rss feed on website asp.net, display rss feed from another site

Display RSS feed on your website ASP.Net

In this asp.net tutorial I will cover the following topics:-

  • What is RSS feed
  • RSS feed features
  • How to display RSS feed on website with images

What are RSS Feed and It’s features

RSS stands for Rich Site Summary or somewhere known as Really Simple Syndication. It is a format for delivering regularly changing web content which is written in XML. There are many free RSS feed creator are available online like Google Feedburner, feedity etc. Using RSS you can easily publish regularly updated web content such as blogs, articles, and news etc.

Many news-related sites, weblogs, and other online publishers syndicate their content as an RSS feed to whoever wants it. RSS Feeds have following features:-

  • Allows you to syndicate your site content
  • RSS files can be automatically updated
  • Defines an easy way to share and view headlines and content
  • Allows personalized views for different sites

HTML code to display RSS feed on website

In the context of displaying content of RSS feeds on your website, custom RSS feed, display RSS feeds from other sites, fetch and display RSS feeds, consuming RSS feed using asp.net, online RSS news aggregator with asp.net. Find the below HTML code to display RSS feed on website:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Display rss feed in asp.net page</title>
    <link href="CSS/bootstrap.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .jobs-well {
            padding: 20px;
            margin: 20px 0;
            border: 1px solid #eee;
            border-left-width: 5px;
            border-radius: 3px;
            border-left-color: #0288d1;
        }

            .jobs-well h4 {
                color: #0288d1;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <div class="row">
                <div class="col-md-12 ">
                    <br />
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h4>How to Read and Display RSS Feed in ASP.NET C#</h4>
                        </div>

                        <div class="panel-body">
                            <div class="form-group">
                                <label>Feed URL</label>
                                <asp:TextBox ID="txt_url" type="URL" CssClass="form-control" placeholder="Feed URL" runat="server" required></asp:TextBox>
                            </div>
                            <div class="form-group">
                                <label>Feed Type</label>
                                <asp:DropDownList ID="drp_typ" CssClass="form-control" runat="server">
                                    <asp:ListItem Value="RSS">RSS</asp:ListItem>
                                    <asp:ListItem Value="OTHER">OTHER</asp:ListItem>
                                </asp:DropDownList>
                            </div>
                        </div>

                        <div class="panel-footer">
                            <asp:Button ID="Button2" runat="server" CssClass="btn btn-success"
                                Text="Go!" OnClick="Button2_Click" />
                        </div>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="panel panel-warning">
                        <div class="panel-heading">
                            <h4>Feed Details </h4>
                        </div>

                        <div class="panel-body">
                            <asp:Repeater ID="Repeater1" runat="server">
                                <ItemTemplate>
                                    <div class="jobs-well">
                                        <h4><%#Eval("Title") %></h4>
                                        <div class="cnt" style="overflow: hidden"><%#Eval("Content") %></div>

                                        <hr />
                                        <a href='<%#Eval("Link") %>' target="_blank" class="btn btn-success"><i class="fa fa-check"></i>View Details</a>
                                    </div>
                                </ItemTemplate>
                            </asp:Repeater>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </form>
</body>
</html>

using System;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
   
    }
    void show()
    {
        FeedParser parser = new FeedParser();
      
        if (drp_typ.SelectedItem.Value == "OTHER")
        {             
        }        
        else if (drp_typ.SelectedItem.Value == "RSS")
        {
           var items = parser.Parse(txt_url.Text, FeedType.RSS);
           Repeater1.DataSource = items;
           Repeater1.DataBind();
        }    
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        show();
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.ServiceModel.Syndication;
/// <summary>
/// Summary description for FeedParser
/// </summary>
public class FeedParser
{
    /// <summary>
    /// Parses the given <see cref="FeedType"/> and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
    /// </summary>
    /// <returns></returns>
    public IList<Item> Parse(string url, FeedType feedType)
    {
        return ParseRss(url);        
    }

    /// <summary>
    /// Parses an RSS feed and returns a <see cref="IList&amp;lt;Item&amp;gt;"/>.
    /// </summary>
    public virtual IList<Item> ParseRss(string url)
    {
        try
        {
            XDocument doc = XDocument.Load(url);
            // RSS/Channel/item
            var entries = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
                          select new Item
                          {
                              FeedType = FeedType.RSS,
                              Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
                              Link = item.Elements().First(i => i.Name.LocalName == "link").Value,
                              PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "pubDate").Value),
                              Title = item.Elements().First(i => i.Name.LocalName == "title").Value
                          };
            return entries.ToList();
        }
        catch
        {
            return new List<Item>();
        }
    }

    private DateTime ParseDate(string date)
    {
        DateTime result;
        if (DateTime.TryParse(date, out result))
            return result;
        else
            return DateTime.MinValue;
    }
}

public enum FeedType
{
    /// <summary>
    /// Really Simple Syndication format.
    /// </summary>
    RSS
}

public class Item
{
    public string Link { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime PublishDate { get; set; }
    public FeedType FeedType { get; set; }

    public Item()
    {
        Link = string.Empty;
        Title = string.Empty;
        Content = string.Empty;
        PublishDate = DateTime.Today;
        FeedType = FeedType.RSS;
    }
}

Download Code

What do you think?

I hope you liked this article on display rss feed asp net c# example. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

LEAVE A REPLY

Please enter your comment!
Please enter your name here