Latest

random

How to display RSS feed on Website - C#

RSS Feed Reader ASP.Net C#

RSS stands for Rich Site Summary and also known as Really Simple Syndication. There are many free RSS feed/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. 

In this article we will explore many more about rss feeds. 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.

display fresh content on websites, custom RSS feed,Displaying RSS Feeds, display RSS feeds from other sites, Fetch and Display RSS Feeds, read and display RSS FEED, Display Headlines From an RSS Feed on Your Website, Consuming RSS Feed using ASP.NET, Online RSS News Aggregator with ASP.NET, displaying XML data in an ASP.NET Web page, display your favourite RSS feed on your website
Reading RSS Feed - ASP.Net
For example: Considering some of you like visiting to www.etechpulse.com to check for new asp.net articles, code snippets or source code. Instead what you can do is subscribe to my website's rss feed over here : -

After visiting the above rss feed url, you will be able to find an XML, because RSS is nothing but an XML file. So I will demonstrate how to read that RSS feed XML and display fresh content in asp.net GridView control using C#. Find the code below :- 
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to display RSS feed on Website - C#</title>
</head>
<body>
    <form id="form1" runat="server">
        <h3>RSS Feed Reader ASP.Net C#</h3>
        <div style="max-height: 600px; overflow: auto">
            <asp:GridView ID="gvRss" runat="server" AutoGenerateColumns="false" ShowHeader="false" Width="90%">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <table style="width: 100%; border: 0;">
                                <tr>
                                    <td>
                                        <h3 style="color: #3E7CFF"><%#Eval("Title") %></h3>
                                    </td>
                                    <td style="width: 200px">
                                        <%#Eval("PublishDate") %>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="2">
                                        <hr />
                                        <%#Eval("Description") %>
                                    </td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td style="text-align: right;">
                                        <a href='<%#Eval("Link") %>' target="_blank">Read More...</a>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<RssFeedItem> rssFeedItems = new List<RssFeedItem>();
            rssFeedItems = ReadFeed("http://www.etechpulse.com/feeds/posts/default?alt=rss&max-results=5");
            gvRss.DataSource = rssFeedItems;
            gvRss.DataBind();
        }
    }
    public static List<RssFeedItem> ReadFeed(string url)
    {
        //create a new list of the rss feed items to return
        List<RssFeedItem> rssFeedItems = new List<RssFeedItem>();

        //create an http request which will be used to retrieve the rss feed
        HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(url);

        //use a dataset to retrieve the rss feed
        using (DataSet rssData = new DataSet())
        {
            //read the xml from the stream of the web request
            rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());

            //loop through the rss items in the dataset and populate the list of rss feed items
            foreach (DataRow dataRow in rssData.Tables["item"].Rows)
            {
                rssFeedItems.Add(new RssFeedItem
                {
                    ChannelId = Convert.ToInt32(dataRow["channel_Id"]),
                    Description = Convert.ToString(dataRow["description"]),
                    ItemId = Convert.ToInt32(dataRow["item_Id"]),
                    Link = Convert.ToString(dataRow["link"]),
                    PublishDate = Convert.ToDateTime(dataRow["pubDate"]),
                    Title = Convert.ToString(dataRow["title"])
                });
            }
        }
        //return the rss feed items
        return rssFeedItems;
    }
}
Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for RSSFeedItem
/// </summary>
public class RssFeedItem
{
    /// <summary>
    /// Gets or sets the title
    /// </summary>
    public string Title { get; set; }

    /// <summary>
    /// Gets or sets the description
    /// </summary>
    public string Description { get; set; }

    /// <summary>
    /// Gets or sets the link
    /// </summary>
    public string Link { get; set; }

    /// <summary>
    /// Gets or sets the item id
    /// </summary>
    public int ItemId { get; set; }

    /// <summary>
    /// Gets or sets the publish date
    /// </summary>
    public DateTime PublishDate { get; set; }

    /// <summary>
    /// Gets or sets the channel id
    /// </summary>
    public int ChannelId { get; set; }
}
I hope you will enjoy the development tips while programming with C# RSS feeds. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. 

Also If you like this article, don't forget to share this article with your friends and colleagues.

No comments:

All Rights Reserved by Etechpulse © 2015 - 2016
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.