4,616,077 members and growing!   7,375 now online.
Email Password Password problem?
All Topics, C#, .NET >> C# Programming >> General  (Beginner)

Really Simple RSS (Yeah, I know)
By Ennis Ray Lynch, Jr..

An easy way to publish your own news feed in ASP.NET
C#, XML
Windows, .NET
ASP.NET, Win32, VS, WebForms
Dev
Posted: 5 Oct 2007
Updated: 15 Oct 2007
Views: 6,631
Announcements


Search    
Advanced Search
Sitemap
PrintBroken Article?Bookmark Discuss Send to a friend
8 votes for this article.
Popularity: 3.54. Rating: 3.92 out of 5.

Introduction

There are plenty of articles out there dealing with consuming RSS, but only a few dealing with making your own. Furthermore, every time I use the xsd.exe tool published by Microsoft, I find people who have never heard of it. Despite its limitations, it is a good tool that needs more mention.

One thing I would like to mention up front is that XML and XSD are usually camelCase and not pascalCase. Unfortunately, the xsd.exe tool generates a blind copy of casing. If you manually edit the generated *.cs file below, you can change the attributes and the casing to get pascalCase .NET code. The output will still be compliant camelCase RSS, i.e.

[System.Xml.Serialization.XmlElement("rss")]
public partial class RSS {
    ...

I don't use this method in this article, in favor of generatability. With that said, I humbly present a simple XSD file and some code which, when used, will make it really simple to add really simple syndication to your site.

Background

This Wikipedia Article on RSS is the best background, I think.

Code

I skipped a lot of the standards for RSS 2.0 to make life easier. If you find you need a field that the schema below doesn't provide, then add it and regenerate the class file. Basically, copy and paste the file below into a file on your machine called C:\RSS_20.xsd (chosen for the article's sake; change at will).

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="unqualified" version="2.0.1.10">
    <xs:annotation>
        <xs:documentation>Simple XML Schema for RSS 2.0</xs:documentation>
    </xs:annotation>
    <xs:element name="rss">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="channel">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="link" type="xs:anyURI" />
                            <xs:element name="description" 
                                type="xs:string" />
                            <xs:element name="language" type="xs:string" />
                            <xs:element name="pubDate" type="xs:dateTime" />
                            <xs:element name="lastBuildDate" 
                                type="xs:dateTime" />
                            <xs:element name="docs" type="xs:string" />
                            <xs:element name="generator" type="xs:string" />
                            <xs:element name="managingEditor" 
                                type="xs:string" />
                            <xs:element name="webMaster" type="xs:string" />
                            <xs:element name="item" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="title" 
                                            type="xs:string" />
                                        <xs:element name="link" 
                                            type="xs:string" />
                                        <xs:element name="description" 
                                            type="xs:string" />
                                        <xs:element name="pubDate" 
                                            type="xs:dateTime" />
                                        <xs:element name="guid" 
                                            type="xs:string" />
                                    </xs:sequence>      
                                </xs:complexType> 
                            </xs:element>
                        </xs:sequence>    
                    </xs:complexType>   
                </xs:element>
            </xs:sequence>
            <xs:attribute name="version" type="xs:string"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Now the fun part. Run a Visual Studio command prompt. There should be a shortcut installed from the Start menu -> Visual Studio -> Visual Studio Tools. If you cannot find it, then just open a command prompt and change your path Environment Variable. While I cannot tell you what your path will be, the one on my current machine is C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727. Your mileage may vary. For those that don't know, that path command is SET PATH=C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin with no space between the equals.

  • In the command prompt, type C: and press Enter
  • In the command prompt, type cd\ and press Enter
  • In the command prompt, type xsd.exe RSS_20.xsd -c /n:"My.Favorite.Namespace" and press Enter

You should now have a RSS_20.cs file created.

//--------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.832
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------

...
snip
...

Add the file to your favorite C# project in ASP.NET. Create a sample page with only the page load method wired up and add the code below.

private rss FakeRss(){
    rss someRss = new rss();
    someRss.version = "2.0";
    someRss.channel = new rssChannel();
    someRss.channel.description = "A nice description for the channel";
    someRss.channel.language = "en-us";
    someRss.channel.link = "http://www.erlglobal.com";
    someRss.channel.title = "The Title of my feed";
    someRss.channel.pubDate = DateTime.Now.ToUniversalTime();

    rssChannelItem channelItem = new rssChannelItem();
    channelItem.pubDate = DateTime.Now.ToUniversalTime();
    channelItem.title = "News Item Title";
    channelItem.link = "http://www.a.link.to.the.item";
    channelItem.guid = "Usually the link but make it unique";
    channelItem.description = "A Brief Summary";
    someRss.channel.item = new rssChannelItem[]{channelItem};

    return someRss;
}

protected void Page_Load(object sender, EventArgs e) {
    Response.Clear();
    Response.ContentType = "text/xml";
    Response.ContentEncoding = System.Text.Encoding.UTF8;
    XmlSerializer XML = new XmlSerializer(typeof(rss));
    xml.Serialize(Response.Output, FakeRss());
    Response.End();
}

And there you go: an RSS feed that you can deliver from your own site, using a schema that can be validated against illustrating the xsd.exe tool.

Points of Interest

History

  • 9 October, 2007 -- Removed error in the .Xsd, thanks pzkpfw
  • 5 October, 2007 -- Original version posted

About Ennis Ray Lynch, Jr.


I have been a software consultant since 1999. I provide C# Consulting Services through my corporation, ERL Global, Inc. One day I may even hire my own developers until then it is just myself and my network of consultant colleagues.

Click here to view Ennis Ray Lynch, Jr.'s online profile.


Other popular C# Programming articles:




[Top] Sign in to vote for this article:     PoorExcellent  
Attention: You must confirm your email address before you can post to this forum.
Note: You must Sign in to post to this message board.
Hint: For a faster board use IE 4+ or Mozilla, choose 'Message View' from the View dropdown and hit 'Set Options'.
FAQ Message score threshold    Search comments  
 View   Per page  
 Msgs 1 to 6 of 6 (Total: 6) (Refresh)[First] [Prev] [Next] [    ]
Subject 
Author 
Date 
  Nice quick jumpstart
 pzkpfw 16:51 9 Oct '07 
  Rofl
 Ennis Ray Lynch, Jr. 17:56 9 Oct '07 
  Little Dissapointed
 MikJr 23:03 8 Oct '07 
  Re: Little Dissapointed
 Ennis Ray Lynch, Jr. 9:39 9 Oct '07 
  RSS 2.0 Schema
 pulsar 6:50 7 Oct '07 
  Re: RSS 2.0 Schema
 Ennis Ray Lynch, Jr. 19:53 7 Oct '07 
Last Visit: 19:33 Friday 19th October, 2007[First] [Prev] [Next] [    ]

General comment    News / Info    Question    Answer    Joke / Game    Admin message


Updated: 15 Oct 2007 Article content copyright Ennis Ray Lynch, Jr., 2007
everything else Copyright © CodeProject, 1999-2007.
Web17 | Advertise on The Code Project | Privacy

The Ultimate ToolboxASP AllianceDeveloper FusionDevelopersdexDevGuruProgrammers HeavenPlanet Source CodeTek-Tips Forums