Friday, April 24, 2009

RSS Feed With ASP.NET



We can easily integrate XML feeds into our website very easily. We don't want to know detail knowledge of XML, but knowledge not harm in any ways.for this wrk we wanr to conntrols
1). XMDataSource Control
2). GridView Control.

We have to set the following properties of the XMDataSource Control
1. DataFile- to the file where XML feeds Resides example: http://feeds.feedburner.com/NysecomFina ... wsReleases

2). set XPath="rss/channel/item"

so we have done for XMDataSource Control properties
it will look like the below code

<asp:XmlDataSource ID="xmlBanking" runat="server" DataFile="http://feeds.feedburner.com/NysecomFinancialNewsReleases"
XPath="rss/channel/item"></asp:XmlDataSource>

Next we move to the making settings of GridView control properties
1). Place a GridView Control on the webpage
2). change the "DataSourceID" according to the ID of our XMLDataSource.
3). Set the AutoGenerateColumns="False", as we are placing our own controls in the GridView
4). Create a TemplateField column and place ItemTemplatein it and the following controls like label, Hyperlink and set the properties given,
in label(property text)='<%# XPath("pubDate") %>'
in Hyperlink, set text='<%# XPath("title") %>' and NavigateUrl='<%# XPath("link") %>'
it will look like below

<asp:Label ID="Label1" runat="server" Text='<%# XPath("pubDate") %>' ForeColor="gray"
Font-Bold="True" Font-Names="Verdana" Font-Size="XX-Small">
</asp:Label>
<br />
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# XPath("title") %>' NavigateUrl='<%# XPath("link") %>'
Target="_blank" Font-Names="Verdana" Font-Size="X-Small">
</asp:HyperLink>

and do for AlternatingItemTemplate also

the whole GridView will be like that:

<asp:GridView ID="bank" runat="server" DataSourceID="xmlBanking" BorderWidth="0"
AutoGenerateColumns="False" CellPadding="2" ForeColor="Black" AllowPaging="True"
PageSize="5">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# XPath("pubDate") %>' ForeColor="gray"
Font-Bold="True" Font-Names="Verdana" Font-Size="XX-Small">
</asp:Label>
<br />
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# XPath("title") %>' NavigateUrl='<%# XPath("link") %>'
Target="_blank" Font-Names="Verdana" Font-Size="X-Small">
</asp:HyperLink>
<br />
<asp:Label ID="Label2" runat="server" Text='<%# XPath("title") %>' ForeColor="gray"
Font-Bold="True" Font-Names="Verdana" Font-Size="XX-Small">
</asp:Label>
</ItemTemplate>
<AlternatingItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# XPath("pubDate") %>' ForeColor="gray"
Font-Bold="True" Font-Names="Verdana" Font-Size="XX-Small">
</asp:Label>
<br />
<asp:HyperLink ID="HyperLink2" runat="server" Text='<%# XPath("title") %>' NavigateUrl='<%# XPath("link") %>'
Target="_blank" Font-Names="Verdana" Font-Size="X-Small">
</asp:HyperLink>
<br />
<asp:Label ID="Label2" runat="server" Text='<%# XPath("title") %>' ForeColor="gray"
Font-Bold="True" Font-Names="Verdana" Font-Size="XX-Small">
</asp:Label>
</AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

and below is the whole page for this article

Code: Select all
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="WebUserControl.ascx" TagName="test" TagPrefix="dd" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="bank" runat="server" DataSourceID="xmlBanking" BorderWidth="0"
AutoGenerateColumns="False" CellPadding="2" ForeColor="Black" AllowPaging="True"