How to display rss feed in asp.net

In this post I will show you how to display rss feed in asp.net using XmlDataSource and DataList control. Rss_cs.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RSS_CS.aspx.cs" Inherits="RSS_CS" %>

<!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:TextBox ID="txtUrl" runat="server" Width="349px"></asp:TextBox>
        <asp:Button ID="btnLoad" runat="server" Text="Load" OnClick="btnLoad_Click" />
        <br />
        <asp:DataList ID="dlRss" runat="server">
            <ItemTemplate>
                <%#XPath("title") %><br />
                <i>
                    <%#XPath("description") %></i> <i>
                        <br />
                        <%#XPath("pubdate") %></i> <b><a href='<%#XPath("link") %>'>
                            <%#XPath("link") %></a></b><br />
            </ItemTemplate>
        </asp:DataList>
        <asp:XmlDataSource ID="xml" runat="server" XPath="rss/channel/item"></asp:XmlDataSource>
    </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RSS_CS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
    }
    protected void btnLoad_Click(object sender, EventArgs e)
    {
        
        if (txtUrl.Text != "")
        {
            xml.DataFile = txtUrl.Text;
            dlRss.DataSourceID = xml.ClientID;
        }
    }
}

No comments:

Post a Comment