Click here to Skip to main content
13,301,100 members (62,300 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 7 Aug 2007

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


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 = "";
string Password = "xxxxxx";
string UserAccount = "xxxxxx";
string DomainName = "xxxxxx";
string ProxyServer = "";
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();

    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="">
    <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">

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

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


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


  • 7th August, 2007: Initial post


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

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Debopam Pal16-Nov-13 21:15
memberDebopam Pal16-Nov-13 21:15 
GeneralHere're the improvements in the code Pin
Cuchuk Sergey21-Apr-08 2:10
memberCuchuk Sergey21-Apr-08 2:10 
<br />
    private static string rssRead(string link, string path)<br />
    {<br />
        // Create the XslCompiledTransform object.<br />
        XslCompiledTransform xslt = new XslCompiledTransform();<br />
<br />
        // Create a resolver and set the credentials to use.<br />
        XmlUrlResolver resolver = new XmlUrlResolver();<br />
        resolver.Credentials = CredentialCache.DefaultCredentials;<br />
<br />
        // Load the style sheet.<br />
        //xslt.Load("RSS91.xsl", XsltSettings.Default, resolver);<br />
        xslt.Load( path + @"\RSS91.xsl", XsltSettings.TrustedXslt, resolver);<br />
<br />
        StringBuilder builder = new StringBuilder();<br />
        // Transform the file.<br />
        XmlWriterSettings wrs = new XmlWriterSettings();<br />
        wrs.ConformanceLevel = ConformanceLevel.Auto;<br />
        using (XmlWriter writer = XmlWriter.Create(builder, wrs))<br />
        {<br />
            xslt.Transform(link, writer);<br />
        }<br />
<br />
        return builder.ToString();<br />
    }<br />

and some changes in the form
<br />
    protected void Button1_Click(object sender, EventArgs e)<br />
    {<br />
        _rezultLabel.Text = rssRead(_webLink.Text, Server.MapPath(@".\"));<br />
    }<br />

and some changes in the form

<br />
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><br />
<br />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ""><br />
<html xmlns=""><br />
<head runat="server"><br />
    <title>Untitled Page</title><br />
</head><br />
<body><br />
    <form id="form1" runat="server"><br />
        &nbsp;??????? ?????:<div><br />
            &nbsp;<asp:TextBox ID="_webLink" runat="server" Width="626px"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Process" Width="130px" /><br /><br />
            <asp:Label ID="_rezultLabel" runat="server" Height="505px" Text="??????" Width="780px"></asp:Label></div><br />
    </form><br />
</body><br />
</html><br />

QuestionThe remote server returned an error: (502) Bad Gateway. Pin
Member 1646461-Apr-08 3:27
memberMember 1646461-Apr-08 3:27 
GeneralRe: The remote server returned an error: (502) Bad Gateway. Pin
NiteShade13-Apr-08 23:59
memberNiteShade13-Apr-08 23:59 
GeneralRe: The remote server returned an error: (502) Bad Gateway. Pin
Member 844439423-Jan-13 1:03
memberMember 844439423-Jan-13 1:03 
GeneralRefactor into class Pin
Ben Daniel7-Aug-07 14:43
memberBen Daniel7-Aug-07 14:43 
GeneralRe: Refactor into class Pin
Raymund Macaalay7-Aug-07 16:07
memberRaymund Macaalay7-Aug-07 16:07 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 7 Aug 2007
Article Copyright 2007 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid