..
In a previous article we saw how easy it is to transform XML with XSL in ASP.NET , this system, however, does not transform a RSS feed , since the XML file, in this case, is not on the same server run the script, but on a remote server.
So let's see how to read an RSS feed with ASP.NET, using XSL transformation to run and manage the formatting of the final output to video.
Before moving to ASP.NET code to use (which will use the namespaces System.IO, System.Xml and System.Xml.Xsl) we take a look at the XSL code that will be contained in the file feed.xsl and placed in the same folder as you will find the ASP.NET file that will run the script:
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<xsl:for-each select="rss/channel/item">
<p>
<a href="{link}"> <b> <xsl:value-of select="title" /> </ b> </ a> <br />
<xsl:value-of select="description" /> <br />
Written by <i> <xsl:value-of select="author" /> on <xsl:value-of select="pubDate" /> </ i>
</ P>
</ Xsl: for-each>
</ Xsl: template>
</ Xsl: stylesheet>
Let us go then to extract from the node that contains the news - or "rss / channel / item" - the fields title, description, author, and pubDate which contain, respectively, the data for the title, description, author and date on which the news was written.
Move on to write ASP.NET code that uses the XmlDocument and XslTransform classes, so objects StringBuilder and StringWriter.
Here's the complete code:
<% @ Page Language = "VB"%>
<% @ Import Namespace = "System.IO"%>
<% @ Import Namespace = "System.Xml"%>
<% @ Import Namespace = "System.Xml.Xsl"%>
<script language="VB" runat=server>
Sub Page_Load (sender As Object, e As EventArgs)
Dim As XmlDocument = New XmlDocument CaricaXML ()
CaricaXML.Load ("/rss/news.xml")
Dim As XslTransform = New XslTransform CaricaXSL ()
CaricaXSL.Load (Server.MapPath ("feed.xsl"))
Dim sb As StringBuilder = New StringBuilder ()
Dim sw As StringWriter = New StringWriter (sb)
CaricaXSL.Transform (CaricaXML, Nothing, sw)
risultato.Text = sb.ToString ()
End Sub
</ Script>
<html>
<head>
<title> Read an RSS Feed using ASP.NET and XSL </ title>
</ Head>
<body>
id="modulo" <form runat="server">
<asp:literal id="risultato" runat="server" />
</ Form>
</ Body>
</ Html>
Once you retrieve the remote RSS feeds (for example, we chose one of the RSS Feed centre-equestre-lepuy.com ) the load and then load the XSL document that will, as mentioned above, the formatting.
We perform the transformation and print the results as a tag text made available by the server side. NET Framework.
| |
ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €. |
| |
ASP.NET (Course)
Full course for building Web applications from 49 €. |
| |
SQL and Database (Course)
Create and manage relational databases. From 39 €. |