Click here to Skip to main content
6,635,160 members and growing! (16,173 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

Simple RSS Reader in C#

By Raymund Macaalay

Simple RSS Reader in C# using XSL
XML, XSLT, C# 2.0.NET 2.0, WinXPVS2005, Dev
Posted:7 Aug 2007
Views:17,116
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
5 votes for this article.
Popularity: 2.45 Rating: 3.50 out of 5

1
1 vote, 20.0%
2
1 vote, 20.0%
3
2 votes, 40.0%
4
1 vote, 20.0%
5
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">
        <!-- Do not show channel image -->
        <xsl:for-each select="channel/item">
            <br>

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

                <!-- only display markup for description if it's present -->
                <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


Member

Location: New Zealand New Zealand

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralHere're the improvements in the code PinmemberCuchuk Sergey2:10 21 Apr '08  
QuestionThe remote server returned an error: (502) Bad Gateway. PinmemberMember 1646463:27 1 Apr '08  
GeneralRe: The remote server returned an error: (502) Bad Gateway. PinmemberNiteShade23:59 13 Apr '08  
GeneralRefactor into class PinmemberBen Daniel14:43 7 Aug '07  
GeneralRe: Refactor into class PinmemberRaymund Macaalay16:07 7 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 7 Aug 2007
Editor: Deeksha Shenoy
Copyright 2007 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project