Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 11th, 2007, 04:30 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default 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() &lt; 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."
===============================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rss feeds SKhna ASP.NET 2.0 Basics 0 April 17th, 2008 05:00 AM
Rss feeds SKhna ASP.NET 2.0 Basics 3 March 20th, 2008 02:07 PM
RSS Feeds with JavaScript NEO1976 Javascript How-To 0 October 6th, 2006 09:13 AM
RSS feeds from the forum? javatis Forum and Wrox.com Feedback 1 February 2nd, 2006 03:44 PM
RSS Feeds harpua PHP How-To 4 October 21st, 2005 09:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.