How to get rss feed of google News or other XML in ASP.net

Create a gridview in your html as follows:

      <asp:GridView ID="gvln1" runat="server" AutoGenerateColumns="False" 
            CellPadding="1" ForeColor="#333333"
            GridLines="None" Style="position: static" 
             EmptyDataText="Unable to Fetch News Item from Internet.Please Try Again Later." Font-Size="12px" Width="180px" BorderColor="#3399FF" 
                      BorderStyle="Dotted" BorderWidth="0px" AllowPaging="true" 
             ShowHeader="False"  Caption="Source: Google News">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerSettings Visible="False" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>

          <asp:TemplateField  ShowHeader="False">
         
          <ItemTemplate>
                 <%# XPath("item")%><br />
               <a href="<%# XPath("link")%>" style="text-decoration:none;" target="_blank"> <%# XPath("title")%></a>
               - <font color="#ADADAD" size="1"><%# XPath("pubDate")%></font>
               <br /> 
              
             </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>


Now in code behind create a function and call it like this with url and gridview as parameters:
Call:
 getRSSFeed("http://news.google.co.in/news?hl=en&gl=in&q=ntpc&um=1&ie=UTF-8&output=rss", gvln1)

Private Function getRSSFeed(ByVal url As String, ByVal gvLiveNews As GridView) As String
        Dim cachename = "news"
        Dim myxml As XmlDataSource = New XmlDataSource


        If IsNothing(Cache(cachename)) Then
            '  div_err.InnerHtml = "if cache is null then fetch"
            myxml = New XmlDataSource  'myxml is null so reset it
            myxml.DataFile = url
            myxml.XPath = "rss/channel/item"

            Try
                Cache.Insert(cachename, myxml, Nothing, Now.AddMinutes(20), TimeSpan.Zero)
                'nameof ur cache, datasource, null , no of minutes or hours to survive, timespan
                gvLiveNews.DataSource = myxml
                gvLiveNews.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
            myxml = CType(Cache.Item(cachename), XmlDataSource)
            '  div_err.InnerHtml = "Getting from cache"
            gvLiveNews.DataSource = myxml
            gvLiveNews.DataBind()
            Return "1"
        End If
    End Function



Output:

Comments

Popular Posts