HOW-TO: Read and Display RSS Feeds
It is somewhat pointless to be posting this as there are many free RSS Readers out there that you can plug into your website but I have had a few messages come from this board as of late on how to do this so here goes.
Language: C# (Basic concept of XML and XSL are needed as well)
IDE: Visual Studio 2003
Difficulty: Beginner
Eventually what we want to be able to do is something like this:
pnlRSS.Controls.Add(gr.getRSS("[url of rss feed]", "[path/to/xsl/file]"))
The code that follows is for an application that sits behind a firewall so it utilizes WebRequest and WebResponse.
So create a new class.
public class RssReader
{
public label getRSS(string sXMLUrl, string sXSLUrl){
System.Net.HttpWebRequest httpWebRequest = System.Net.HttpWebRequest.Create(sXMLUrl);
System.Net.WebProxy oProxy = new System.Net.WebProxy("[proxy address]", [proxyport]);
oProxy.Credentials = new System.Net.NetworkCredential("[proxyuser]", "[proxypassword]");
httpWebRequest.Method = "GET";
httpWebRequest.Proxy = oProxy;
httpWebRequest.Accept = "text/xml";
System.Net.HttpWebResponse httpWebResponse = httpWebRequest.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(httpWebResponse.GetResponse Stream());
string stringResult = streamReader.ReadToEnd();
StringReader sr = new StringReader(stringResult);
XmlDocument myXMLDoc = new XmlDocument();
//if this application was not behind a firewall you
//could bypass the above requests and do:
//myXMLDoc.Load(sXMLUrl);
//to retrieve the RSS Feed
myXMLDoc.Load(sr);
//We now have the raw XML loaded into an XMLDocument Object
//Unforunately raw xml doesn't look to good so we need
// to provide a style sheet so it looks a little bit
//nicer.
XslTransform myXSLDoc = new XslTransform();
XmlResolver xmlRez;
StringBuilder stringBuild = new StringBuilder();
StringWriter stringWrit = new StringWriter(stringBuild);
Label lbl = new Label();
// Load the Style
myXSLDoc.Load(sXSLUrl);
// Transform our XML Doc
myXSLDoc.Transform(myXMLDoc, null, stringWrit, xmlRez);
//populate a labe with the transformed document
//and return that label
lbl.Text = stringBuild.ToString;
return lbl;
}
}
So now we could do this:
RssReader gr = new RssReader();
pnlRSS.Controls.Add(gr.getRSS("[url of rss feed]", "[path/to/xsl/file]"))
and viola, a label with a transformed RSS Feed is placed on our website! Below is an example of what an XSL file looks like (more specifically this is the XSL file i use to transform CNN's RSS Feed)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/">
<table border="0" cellpadding="0" width="100%" cellspacing="1" bgcolor="#003366">
<xsl:for-each select="rss/channel/item">
<xsl:if test="position() < 6">
<tr>
<td bgcolor="#ffffff"><a href="{link}" target="new"><strong><xsl:value-of select="title" /></strong></a><Br></Br>
<span class="cnnFeed">
<xsl:value-of disable-output-escaping="yes" select="description" />
</span>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
What this does is grab the first 5 stories from the CNN feed and displays them in a table and then apply the style: cnnFeed which exists in a regular style sheet to the table cells.
Very straight forward isn't it? Let me know if you have any questions!
-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
^^Thats my signature
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|