Consume RSS Feed Using XSLT Transformation in ASP.Net
This is a small tip which helps us to display RSS XML in HTML using XSLT transformation.
Below is the xsl file that has the transformation rules to convert RSS XML to HTML.
RSSXSLT.xslt <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:for-each select="//item"> <div>
<div> <A> <xsl:attribute name="href"> <xsl:value-of select="link" /> </xsl:attribute> <xsl:value-of select="title"/> </A> </div> <div> <xsl:value-of select="description"/> </div>
</div> </xsl:for-each> </xsl:template> </xsl:stylesheet>
The below code will convert the RSS feed to HTML and will display in a Literal Control(ltRss) using XslCompiledTransform class.
string strXSLTFile = Server.MapPath("RSSXSLT.xslt"); XmlReader reader = XmlReader.Create("http://www.codedigest.com/Articles/Rss.ashx"); XslCompiledTransform objXSLTransform = new XslCompiledTransform(); objXSLTransform.Load(strXSLTFile); StringBuilder htmlOutput = new StringBuilder(); TextWriter htmlWriter = new StringWriter(htmlOutput); objXSLTransform.Transform(reader, null, htmlWriter); ltRss.Text = htmlOutput.ToString(); reader.Close();
Include System.Xml.Xsl, System.Xml and System.IO namespace for the above code to work.
Read the below article to know more on XSL and XSLT transformation, Doing XSLT Transformation in ASP.Net
|