ASP.NET Tutorial/Development/RSS

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Displaying an XML RSS blog feed

   <source lang="csharp">

<%@ Page Language="C#"%> <!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 id="Head1" runat="server">
       <title>XmlDataSource</title>
   </head>
   <body>
   <form id="form1" runat="server">
       <asp:DataList ID="DataList1" Runat="server" DataSourceID="XmlDataSource1">
           <HeaderTemplate>
</HeaderTemplate> <ItemTemplate> </ItemTemplate> <AlternatingItemTemplate> </AlternatingItemTemplate> <FooterTemplate>
<%# XPath("title") %>
               <%# XPath("pubDate") %>
<%# XPath("description") %>
<%# XPath("title") %>
               <%# XPath("pubDate") %>
<%# XPath("description") %>
           </FooterTemplate>
       </asp:DataList>
       <asp:XmlDataSource ID="XmlDataSource1" 
                          Runat="server" 
                          DataFile="http://www.hanselman.ru/blog/SyndicationService.asmx/GetRss" 
                          XPath="rss/channel/item">
       </asp:XmlDataSource>
   </form>
   </body>

</html></source>


Rss Reader

   <source lang="csharp">

<%@ 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">

RSS Reader

   Select a RSS feed
<asp:DropDownList ID="drpFeeds" runat="server" > <asp:ListItem Value="http://rss.cnn.ru/rss/cnn_topstories.rss">CNN</asp:ListItem> <asp:ListItem Value="http://msdn.microsoft.ru/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
<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">

<xsl:value-of select="title" />

<xsl:value-of select="description" /> <xsl:for-each select="item">

<xsl:value-of select="title" />

   <p style="font: 10pt Verdana, Helvetica;">
     <xsl:value-of select="description" />
     
<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">

     <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" />
     
   <xsl:for-each select="item">
       <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" />          
       
   </xsl:for-each>

</xsl:template>

</xsl:stylesheet></source>