Create your first RSS feed consumer In .Net

Introduction:

What is RSS Feeds:
RSS known as rich site summary or real simply syndication, which allow you to be updated with content of other site.
In My case suppose I want to see what’s happening in around world so for this purpose we will create a RSS Consumer
that take update from IBN Live , Times of India, and many more news site and display there content on our site to allow
me to update with news. This was one of the example I have taken in account here.

Two parts of RSS
– Create RSS Feed for your site that is creating latest content and keeping that content at one place.
RSS Feed Consumer: Consume content of RSS Feed of another site.

For more information you can read here about RSS Feed http://en.wikipedia.org/wiki/RSS

 Note: Here we have used SyndicationFeed Class if you have not then please download from here first and
reference System.ServiceModel dll for your project.

What we will see here.
– How to consume another site RSS feed item and show on our website. After using below code you will able to see this Image that will be your rss feed consumer.

First Create a user control called ViewFeedBurner.ascx.cs


using System;
using System.Data;
using System.Xml;
using System.Net;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.XPath;
// This namespace contains in System.ServiceModel dll so please download it for it's working.
using System.ServiceModel.Syndication;
using System.Collections.Generic;

namespace FeedBurner.usercontrols
{
public partial class ViewFeedBurner : System.Web.UI.UserControl
 {
  #region Declare Private Properties

  // Declare Feed Url from where data will be retrive.
  private string _feedUrl;
  public string FeedUrl
   {
    get { return _feedUrl; }
    set
   {
   _feedUrl = value;
   }
   }

// Declare No of item to be displayed.
private int _noOfItems;
public int NoOfItems
{
get { return _noOfItems; }
set { _noOfItems = value; }
}

// Declare Title Header of Feed.
private string _feedTitleHeader;
public string FeedTitleHeader
{
get { return _feedTitleHeader; }
set { _feedTitleHeader = value;}
}

#endregion

#region Declare Public Properties

//public SyndicationFeed newsFeed;

#endregion

#region Event Handler

protected void Page_Load(object sender, EventArgs e)
{

// Set Page size.
dataPagerFeeds.PageSize = NoOfItems;
DisplayFeed();
}

#endregion

#region User Define Method

/// <summary>
/// Display Feed
/// </summary>
/// <typeparam name="?"></typeparam>
/// <param name="?"></param>
/// <returns>void</returns>
public void DisplayFeed()
{
try
{
// Read the feed using an XmlReader
XmlReader reader = XmlReader.Create(FeedUrl);
SyndicationFeed feed = SyndicationFeed.Load(reader);

lstViewNewsFeeds.DataSource = feed.Items;
lstViewNewsFeeds.DataBind();

//newsFeedImage.ImageUrl = feed.ImageUrl.AbsoluteUri;
//newsFeedImage.AlternateText = "Image Not Avilable";

lblFeedTitle.Text = FeedTitleHeader;
}
catch (Exception ex)
{
if (ex is WebException || ex is XmlException)
{
// Handle bad url, timeout or xml error here.
}
else
throw;
}
}

/// <summary>
/// Url Validation
/// </summary>
/// <param name="feedUrl">Feed Url</param>
/// <returns>bool</returns>
public bool IsValidUri(string feedUrl)
{
Uri validatedUri;
return Uri.TryCreate(feedUrl, UriKind.RelativeOrAbsolute, out validatedUri);
}

#endregion

/// <summary>
/// Bound List view data item.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lstViewNewsFeeds_ItemDataBound(object sender, ListViewItemEventArgs e)
{
try
{
// Instantiate listview control object.
ListViewDataItem lvDataItem = (ListViewDataItem)e.Item;

//Make sure that data item is binding with listview.
if (lvDataItem.ItemType == ListViewItemType.DataItem)
{

SyndicationItem item = e.Item.DataItem as SyndicationItem;
//SyndicationLink feedItem = e.Item.DataItem as SyndicationLink;

Panel panelNewsDescContent = (Panel)e.Item.FindControl("panelNewsDescription");
HyperLink uriTitleLink = (HyperLink)e.Item.FindControl("hlNewsTitle");

Label lblLastUpdate = (Label)e.Item.FindControl("lblLastUpdateTime");

if (uriTitleLink != null)
{
uriTitleLink.Text = Convert.ToString(item.Title.Text);
uriTitleLink.NavigateUrl = item.Links[0].Uri.AbsoluteUri;
}
// Make sure that panel object is referenced.
if (panelNewsDescContent != null)
{
Label lblBox = new Label();
lblBox.Text = Convert.ToString(item.Summary.Text);
panelNewsDescContent.Controls.Add(lblBox);
}
if (lblLastUpdate != null)
{
lblLastUpdate.Text = Convert.ToString(item.PublishDate);
}
}
}
catch(Exception ee)
{
Response.Write(ee.Message);
}
}
}
}

Now Create ViewFeedBurner.ascx page:
For displaying data we have used list view.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ViewFeedBurner.ascx.cs" Inherits="FeedBurner.usercontrols.ViewFeedBurner" %>
<link href="../css/ViewNewsFeeds.css" rel="stylesheet" type="text/css" />
<div style="float:right">
<%--    <div id="divImageHeader">
<asp:Image ID="newsFeedImage" Height="35px" Width="340px" runat="server" />
</div>--%>
<div id="divFeedTitleHeader">
<asp:Label ID="lblFeedTitle" runat="server"></asp:Label>
</div>
<div id="divMainBar">

<%-- Begin List View --%>
<asp:ListView ID="lstViewNewsFeeds" runat="server" OnItemDataBound="lstViewNewsFeeds_ItemDataBound">
<%-- Bigin: LAYOUT TEMPLATE --%>
<LayoutTemplate>
<div runat="server" id="ItemPlaceHolder" style="width:100%"></div>
<div></div>
</LayoutTemplate>
<%-- End: LAYOUT TEMPLATE --%>

<%-- Bigin: ITEM TEMPLATE --%>
<ItemTemplate>
<%-- Start of the main container --%>
<asp:Panel ID="pnlHeader" runat="server" CssClass="pnlNewsContentPanel">
<asp:HyperLink ID="hlNewsTitle" runat="server"></asp:HyperLink>
<asp:Label ID="lblLastUpdateTime" runat="server"></asp:Label>
<asp:Panel ID="panelNewsDescription" runat="server" CssClass = "pnlNewsDescription"></asp:Panel>
</asp:Panel>
<%-- End of the main container --%>
</ItemTemplate>
<%-- End: ITEM TEMPLATE --%>

<%-- Bigin: EMPTY LAYOUT TEMPLATE --%>
<EmptyDataTemplate>
<div>
Sorry, there are no items to display.
</div>
</EmptyDataTemplate>
<%-- End: EMPTY LAYOUT TEMPLATE --%>
</asp:ListView>
<%-- End List View --%>

<%--List View Pager--%>
<asp:DataPager ID="dataPagerFeeds" runat="server" PagedControlID="lstViewNewsFeeds">
<Fields>
</Fields>
</asp:DataPager>
</div>
</div>

Add CSS File and refer this css file in your ascx page.

 .pnlNewsContentPanel
{
width:341px;
clear:both;
padding:5px;
overflow:hidden;

}
.pnlNewsDescription
{
font-size:14px;
padding:7px;
display:block;
float:right;
width:335px;
color:black;

background-color: #FDFDFD;
border-bottom:1px dotted #DDDDDD;
border-left:1px dotted #DDDDDD;
}
#divMainBar
{
overflow:auto;
background-color: #FDFDFD;
border:none solid #ABD2E9;
height:530px;
width:370px;
}

#hlNewsTitle
{
font-family : Lucida Grande,Lucida Sans,Lucida Sans Unicode,Verdana,Arial,Helvetica,sans-serif;
font-weight: bold;
}
a:link
{
color:blue;
text-decoration: none;
}    /* unvisited link */
a:visited
{
color:#336699;
text-decoration: none;
} /* visited link */
a:hover
{
text-decoration:underline;
}

#divFeedTitleHeader
{
background-color: #F0F0F0;
font-family: "Arial Black",Arial;
font-size:18px;
width:365px;
height:30PX;
text-align:left;
padding-left:5px;
padding-top: 10px;
text-transform: uppercase;
}
#divImageHeader
{
width:370px;
height:40PX;
}

Now create aspx page to show feedburner user control content: and register user control and use above user control and run it will work for you.

Here I have used own I think you have to modify your code for register your user control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="FeedBurner.Test" %>
<%@ Register TagPrefix="CP" TagName="TitleBar" Src="~/usercontrols/ViewFeedBurner.ascx" %>

<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<CP:TitleBar ID="TitleBar1" runat="server" FeedUrl ="http://ibnlive.in.com.feedsportal.com/c/33219/fe.ed/ibnlive.in.com/ibnrss/top.xml" NoOfItems = "10" FeedTitleHeader="Recent IBN News"/>
</div>
</form>
</body>
</html>

If You find any problem and issue regarding above please leave a comment below I will feel happy to help you.
If you like my post then please give your comment in this way I will motivated to share my exp.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s