Rss Reader : RSS « Development « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="RssReader" %>

<!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>Rss Reader</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Panel ID="panSelect" 
               runat="server" 
               BorderWidth="1" 
               BorderStyle="Solid" 
               BackColor="Beige" 
               CssClass="SelectArea">
    <h1>RSS Reader</h1>
    Select a RSS feed<br />    
    <asp:DropDownList ID="drpFeeds" runat="server" >
      <asp:ListItem Value="http://rss.cnn.com/rss/cnn_topstories.rss">CNN</asp:ListItem>
      <asp:ListItem Value="http://msdn.microsoft.com/rss.xml">MSDN</asp:ListItem>
      <asp:ListItem Value="http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/technology/rss.xml">BBC Technology News</asp:ListItem>
    </asp:DropDownList>
    
    Select a template<br />
    <asp:DropDownList ID="drpTemplates" runat="server">
      <asp:ListItem Value="~/App_Data/RssTransform1.xsl">RssTransform 1</asp:ListItem>
      <asp:ListItem Value="~/App_Data/RssTransform2.xsl">RssTransform 2</asp:ListItem>
    </asp:DropDownList>
    
       
          <asp:Button ID="btnRead" runat="server" Text="Read Feed" OnClick="btnRead_Click" />
       
    </asp:Panel>
    
    <asp:Panel ID="panFeed" runat="server">
    <asp:Xml ID="myXml" runat="server"></asp:Xml>
    </asp:Panel>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Xml.XPath;
using System.Xml.Xsl;

public partial class RssReader : System.Web.UI.Page
{
    protected void btnRead_Click(object sender, EventArgs e)
    {
        string xmlUrl = drpFeeds.SelectedValue;
        XPathDocument xpdoc = new XPathDocument(xmlUrl);
        XPathNavigator xnav = xpdoc.CreateNavigator();

        myXml.XPathNavigator = xnav;

        string xslFilename = drpTemplates.SelectedValue;
        myXml.TransformSource = xslFilename;
    }
}

File: RssTransform1.xsl

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/rss/channel">
  <h2 style="font: 14pt Verdana; font-weight: bold; color: #827753; background-color: #CFBD84;">
    <xsl:value-of select="title" />
  </h2>
  <p style="font: 10pt Verdana, Helvetica; margin-bottom: 10px;">
    <xsl:value-of select="description" />
  
  <xsl:for-each select="item">
    <h3 style="font: 12pt Verdana, Helvetica; border-bottom: 1px solid #A89A6C;">
      <xsl:value-of select="title" />
    </h3>
    <p style="font: 10pt Verdana, Helvetica;">
      <xsl:value-of select="description" />
      <br/>
      <a href="{link}">
        Read more
      </a>
    
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet> 

File: RssTransform2.xsl

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/rss/channel">
  <div>
    <div style="text-transform: uppercase;">
      <p style="font-size: 22px; color: #930000; margin-bottom: 2px;">
        <xsl:value-of select="title" />
      
      <p style="font-size: 11px; ">
        <xsl:value-of select="description" />
      
    </div>
    <xsl:for-each select="item">
      <div style="background-color: #D1D1D1; padding: 2px; margin-top: 8px;">
        <p style="margin-bottom: 1px; margin-top: 1px; font-size: 16px;">
          <a href="{link}" style=" color: #930000;">
          <xsl:value-of select="title" />
          </a>
        
        <p style="font-size: 10px; margin-top: 1px; margin-bottom: 1px; border-bottom: 1px dashed #949494;">
          <xsl:value-of select="pubDate" />        
        
        <p style="font: 11px Verdana, sans-serif; line-height: 18px; margin-top: 1px; margin-bottom: 1px;">
          <xsl:value-of select="description" />          
        
      </div>
    </xsl:for-each>
  </div>
</xsl:template>

</xsl:stylesheet>








9.37.RSS
9.37.1.Displaying an XML RSS blog feed
9.37.2.Rss Reader