Click here to Skip to main content
Click here to Skip to main content

Simple RSS Reader in C#

, 7 Aug 2007
Rate this:
Please Sign up or sign in to vote.
Simple RSS Reader in C# using XSL
Screenshot - Sample_Page.jpg

Introduction

This is a simple way of reading an RSS from a feed using C# and XSL.

Using the Code

There are two major parts in this solution. One is the Page that requests and renders the feed and another one will be the XSL which transforms the XML feed.

The first is the default.aspx.cs. We need to import the following references first:

using System.Xml;
using System.IO;
using System.Net;

Then let's go to the fun part:

// Declare your Variables
string xmlsrc = "http://rss.news.yahoo.com/rss/topstories";
string Password = "xxxxxx";
string UserAccount = "xxxxxx";
string DomainName = "xxxxxx";
string ProxyServer = "192.168.1.1:8080";
string xslsrc = "RSS91.xsl";

if (xmlsrc != "")
{
    // Make Remote Request
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(xmlsrc);

    if (UserAccount != "")
    {
        wr.Credentials = new NetworkCredential(UserAccount, Password, DomainName);
    }

    // Set Proxy Server
    if (ProxyServer != "")
    {
        wr.Proxy = new WebProxy(ProxyServer, true, new string[] { }, wr.Credentials);
    }

    // Set the HTTP properties
    wr.Timeout = 10000;
    // 10 seconds

    // Read the Response
    WebResponse resp = wr.GetResponse();
    Stream stream = resp.GetResponseStream();

    // Load XML Document
    XmlTextReader reader = new XmlTextReader(stream);
    reader.XmlResolver = null;
    XmlDocument doc = new XmlDocument();

    doc.Load(reader);
    xmlRSS.Document = doc;
    }
xmlRSS.TransformSource = xslsrc;
}

Then the XSL which formats the feed visually. This is how the page will look like the default.aspx just renders it:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:param name="TITLE"/>

    <xsl:template match="rss">
        <!--<span class="code-comment"> Do not show channel image --></span>
        <xsl:for-each select="channel/item">
            <br>

                <strong>
                    <a href="{link}" target="_main">
                        <xsl:value-of select="title"/>
                    </a>
                </strong>
                <br></br>

                <!--<span class="code-comment"> only display markup for description if it's present --></span>
                <xsl:value-of select="description" disable-output-escaping="yes"/>

            </br>
            <br></br>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="description">
        <br>
            <xsl:value-of select="."/>
        </br>
    </xsl:template>
</xsl:stylesheet> 

History

  • 7th August, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 1 PinmemberDebopam Pal16-Nov-13 20:15 
GeneralHere're the improvements in the code PinmemberCuchuk Sergey21-Apr-08 1:10 
QuestionThe remote server returned an error: (502) Bad Gateway. PinmemberMember 1646461-Apr-08 2:27 
GeneralRe: The remote server returned an error: (502) Bad Gateway. PinmemberNiteShade13-Apr-08 22:59 
GeneralRe: The remote server returned an error: (502) Bad Gateway. PinmemberMember 844439423-Jan-13 0:03 
GeneralRefactor into class PinmemberBen Daniel7-Aug-07 13:43 
GeneralRe: Refactor into class PinmemberRaymund Macaalay7-Aug-07 15:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid