How to show live internet content through rss on your web application
Here is sample of web application designed in asp.net vb code behind with ajax. which fetches live news content in english and hindi through rss feed and display on intranet. refreshes each 5 min using updatepanel
VB Code behind
aspx page
VB Code behind
Partial Class livenews
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
loadLiveNews("http://news.google.com/news?cf=all&ned=hi_in&hl=hi&output=rss", gvln1)
loadLiveNews("http://rss.news.yahoo.com/rss/india", gvln2)
'lbError.Text = "in timer"
Timer2.Enabled = True
End Sub
Private Sub loadLiveNews(ByVal url As String, ByVal gvLiveNews As GridView)
XmlDataSource1.DataFile = url '"" 'vinservice.getDataSource
gvLiveNews.DataSource = XmlDataSource1
imgLoad2.Visible = False
Try
lbError.Visible = False
gvLiveNews.DataBind()
Catch ex As Exception
gvLiveNews.Visible = False
lbError.Visible = True
lbError.Text = "Unable to get Live News Feed from internet. Service shall be restored shortly. Sorry for inconvenience."
End Try
End Sub
End Class
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="livenews.aspx.vb" Inherits="livenews" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<br />
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
XPath="rss/channel/item" ></asp:XmlDataSource>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Label ID="lbError" runat="server" Text="Getting News Feed.. Please Wait for few seconds..." > <asp:Image ID="imgLoad2" runat="server" ImageUrl="../images/loaderclock.gif" /> </asp:Label>
<asp:GridView ID="gvln1" runat="server" AutoGenerateColumns="False"
CellPadding="2" ForeColor="#333333"
GridLines="None" Style="position: static"
EmptyDataText="No Users Online" Font-Size="9px" Width="698px" BorderColor="#3399FF"
BorderStyle="Dotted" BorderWidth="1px" AllowPaging="true"
ShowHeader="False" >
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerSettings Visible="False" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<%#XPath("title")%><br />
<%#XPath("pubDate")%><br />
<%#XPath("author")%><br />
<%#XPath("description")%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:GridView ID="gvln2" runat="server" AutoGenerateColumns="False"
CellPadding="2" ForeColor="#333333"
GridLines="None" Style="position: static"
EmptyDataText="No Users Online" Font-Size="9px" Width="695px" BorderColor="#3399FF"
BorderStyle="Dotted" BorderWidth="1px" AllowPaging="true"
ShowHeader="False" >
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerSettings Visible="False" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<%#XPath("title")%><br />
<%#XPath("pubDate")%><br />
<%#XPath("author")%><br />
<%#XPath("description")%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:Timer ID="Timer2" runat="server" Interval="300000">
</asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Above code assumes that your server has direct internet connection. If you have some proxy server for internet then only do following changes in web.config file
In web.config
add following lines
<location path="loader/livenews.aspx">
<system.net>
<defaultProxy >
<proxy
proxyaddress="http://191.254.253.60:8080"
bypassonlocal="True"
/>
</defaultProxy>
</system.net>
</location>
Here is another simple asp.net application to get rss news data and display on a gridview. It also uses caching.
Private Function GetRSSFeed(ByVal link As String, ByVal proxy As String, ByVal gridViewControl As GridView, ByVal cachename As String) As String
'called from timer
Dim myxml As XmlDataSource = New XmlDataSource
myxml = CType(Cache.Item(cachename), XmlDataSource)
If IsNothing(myxml) Then
lbError.Text = "if cache is null then fetch"
myxml = New XmlDataSource 'myxml is null so reset it
myxml.DataFile = link
myxml.XPath = "rss/channel/item"
Try
Cache.Insert(cachename, myxml, Nothing, Now.AddMinutes(2), TimeSpan.Zero)
'nameof ur cache, datasource, null , no of minutes or hours to survive, timespan
gridViewControl.DataSource = myxml
gridViewControl.DataBind()
' lbError.Visible = False
Return "1"
Catch ex As Exception
'gvLiveNews.Visible = False
Return "Unable to get Portion of Live News Feed from internet. Service shall be restored shortly."
End Try
Else
lbError.Text = "Getting from cache"
gridViewControl.DataSource = myxml
gridViewControl.DataBind()
Return "1"
End If
End Function
call this function from a timer and updatepanel to get asynchronus postback.
Protected Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
' loadLiveNews()
GetRSSFeed("http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=ASI|IN|IN033|bhatwari", "191.254.186.111:5678", gvTemp, "gvTemp")
GetRSSFeed("http://www.bseindia.com/sensex/xml-data/sensexrss.xml", "191.254.186.111:5678", gvBSE, "gvBSE")
GetRSSFeed("http://rss.news.yahoo.com/rss/india", "191.254.186.111:5678", gvLiveNews, "gvLiveNews")
GetRSSFeed("http://news.google.com/news?cf=all&ned=hi_in&hl=hi&output=rss", "191.254.253.60:8080", gvLiveNews2, "gvLiveNews2")
Timer2.Enabled = False
End Sub
Comments