Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

RSS 2.0 Framework

Rate me:
Please Sign up or sign in to vote.
4.92/5 (67 votes)
19 Jan 2013LGPL37 min read 505.6K   15.2K   361   107
RSS 2.0 framework implements the RSS 2.0 specification in strongly typed classes. The framework enables you to create and consume valid RSS 2.0 feeds in your code in just a few minutes.

Introduction 

Namespaces

The Raccoom.Xml namespace contains interfaces and classes that deal with RSS 2.1 and OPML 1.1 data. It provides strong typed classes which meet the required specifications. These classes help you to consume or create these kind of documents in your code, no knowledge about XML or the data specifications needed.

The Raccoom.Xml.Editors namespace contains a MDI based editor for RSS and OPML documents. You can load, save and edit your documents. There is also a built-in XML and HTML preview.

Raccoom.Xml.Test contains the NUnit based test cases. Basically, the tests create data and save it to the local storage. After loading into a new instance, it verifies that both instances are equal. Another test scenario takes care of the collection's add and remove methods.

Idea

RSS and OPML documents are XML documents, basically it's all about XML. The main code will parse XML files and store the gained data somewhere. Somewhere? I guess it would be nice and straight forward if I could use data binding, IntelliSense and an intuitive class model to deal with these kind of documents. The worst you can do is implement your XML parser directly into a UI control. But at the end of the day, we would like to present our data in a nice UI. So we have to consume the XML and display the containing data in a nice way. Like always, we should separate data from UI. That's where this framework comes in...

Design

Common

  • Every element has its corresponding interface/class implementation.
  • Solid design with strong typed classes and interfaces.
  • Strong typed collections used to connect classes together.
  • Comprehensive built-in documentation.
  • Comprehensive built-in TypeConverter and UITypeEditors for tricky values like RFC 822 DateTime and ISO Culture.
  • The Parser uses reflection to set the matching attributes from XML file and class instance.
  • The Parser is built with a non cached, fast, forward-only reading XmlTextReader.

Persistent Manager

Both document types implement the persistence manager interface. So if you are familiar with the interface methods, you can work with both document types. Basically, this interface is responsible to save, publish and transform the data.

Image 1

Dirty State

Every class provides a PropertyChanged event which is raised when a class property has changed its value. The PropertyChangedEventArgs object specifies the name of the property that has changed. The property names are stored in a nested fields class that provide string constants for every property. If a child element is attached to a parent element, the parent element consumes the child's PropertyChanged event, this way the top level class will receive and pass on the dirty event for all nested elements. You just have to consume the PropertyChanged event on the top level class.

XML Parser

The main work was to write the parser. Because XML files are read non cached and only forward (performance issue), it's a little bit tricky. The main purpose was to be able to read every well formatted RSS 2.0 feed, no matter which element order or which elements really exist in the XML file. The most code I found on the net parse the XML file with an expected element order, or does support just a few elements.

Every class type in the framework supports two constructors. The standard constructor has no parameters and initializes the class instance with default values. The second uses an XmlTextReader instance, whose current position/state must meet the specific class type element, to initialize the class instance. Only the top level classes RssChannel and OpmlDocument provide a different second constructor. The top level classes provide overloaded constructors to initialize a new class instance from a URI or a file stream where the parser starts.

Initialize a new instance of the RssCloud class.

C#
public RssCloud();
public RssCloud(XmlTextReader);

The implementation depends on the element type. Both parser work modes are based on the idea to read forward and find the corresponding properties on the current class instance via reflection.

Most elements contain XmlElementAttributes:

C#
public RssImage(XmlTextReader xmlTextReader)
{
    System.Diagnostics.Debug.Assert(!xmlTextReader.IsEmptyElement);
    System.Reflection.PropertyInfo propertyInfo = null;
    while(!(xmlTextReader.Name == "image" && xmlTextReader.NodeType ==
    XmlNodeType.EndElement))
    {
        // Continue read
        xmlTextReader.Read();
        // find related property by name
        propertyInfo = GetType().GetProperty(xmlTextReader.Name,
          System.Reflection.BindingFlags.IgnoreCase |
          System.Reflection.BindingFlags.Public |
          System.Reflection.BindingFlags.Instance);
        if(propertyInfo!=null)
        {
            // set related property with data
            xmlTextReader.Read();
            propertyInfo.SetValue(this, xmlTextReader.Value,null);
        }
        // read till end tag
        while(xmlTextReader.NodeType!=XmlNodeType.EndElement)
        {
            xmlTextReader.Read();
        }
    }
}

Otherwise they contain XmlAttributes:

C#
public RssCloud(XmlTextReader xmlTextReader)
{
    if(!xmlTextReader.HasAttributes) return;
    System.Reflection.PropertyInfo propertyInfo = null;
    //
    while(xmlTextReader.MoveToNextAttribute())
    {
        // find related property by name
        propertyInfo = GetType().GetProperty(xmlTextReader.Name,
          System.Reflection.BindingFlags.IgnoreCase |
          System.Reflection.BindingFlags.Public |
          System.Reflection.BindingFlags.Instance);
        if(propertyInfo!=null)
        {
            // set related property with data
            propertyInfo.SetValue(this, xmlTextReader.Value,null);
    }
    }
}

RSS

RSS is a web content syndication format. Its name is an acronym for Really Simple Syndication. RSS is a dialect of XML. All RSS files must conform to the XML 1.0 specification, as published on the World Wide Web Consortium (W3C) website. At the top level, a RSS document is a RSS element, with a mandatory attribute called version, that specifies the version of RSS that the document conforms to. Subordinate to the RSS element is a single channel element, which contains information about the channel (metadata) and its contents.

Image 2

C#
// create channel
RssChannel myChannel = new RssChannel();
myChannel.Title = "Sample Feed";
myChannel.Copyright = "email@adress.com";
myChannel.TTL = 20;
// create item
RssItem myRssItem = new RssItem();
myRssItem.Title = "Sample Rss Item";
myRssItem.Link = "http://www.codeproject.com";
myRssItem.Source.Url = "http://www.codeproject.com";
myRssItem.Source.Value = "Codeproject";
// add item
myChannel.Items.Add(myRssItem);
// save feed
myChannel.Save(@"c:\samplefeed.xml");

OPML

The purpose of this format is to provide a way to exchange information between outliners and Internet services that can be browsed or controlled through an outliner. The design goal is to have a transparently simple, self-documenting, extensible and human readable format that's capable of representing a wide variety of data that's easily browsed and edited. As the format evolves, this goal will be preserved. It should be possible for a reasonably technical person to fully understand the format with a quick read of a single Web page. It's an open format, meaning that other outliner vendors and service developers are free to use the format to be compatible with Radio UserLand or for any other purpose.

Image 3

C#
// create document
OpmlDocument myDocument = new OpmlDocument();
myDocument.Head.Title = "Sample document";
myDocument.Head.OwnerEmail = "email@adress.com";
myDocument.Head.OwnerName = "Chris";
// create outline
OpmlOutline myOutlineItem = new OpmlOutline();
myOutlineItem.Text = "Sample Item";
myOutlineItem.Description = "RTFM";
myOutlineItem.XmlUrl = "http://www.codeproject.com/rss.xml";
myOutlineItem.HtmlUrl = "http://www.codeproject.com";
myOutlineItem.IsBreakpoint = false;
myOutlineItem.IsComment = false;
// create sub item
OpmlOutline mySubOutlineItem = new OpmlOutline();
mySubOutlineItem.Text = "Sample Child Item";
// add sub item to item
myOutlineItem.Items.Add(mySubOutlineItem);
// add outline to document
myDocument.Body.Items.Add(myOutlineItem);
// save opml
myDocument.Save(@"c:\sample.opml");

Using the Code

The following samples deal with the RSS classes, but for OPML, it's similar because the operations are mostly based on the IPersistenceManager interface.

Consume Content from Internet

This sample shows how to consume The Code Project "Last 10 updates (category: All Topics)" RSS feed.

C#
Uri uri = new
  Uri("http://www.codeproject.com/webservices/articlerss.aspx?cat=1");
RssChannel myRssChannel = new RssChannel(uri);
// write the channel title to the standard output stream.
System.Console.WriteLine(myRssChannel.Title);
// write each item's title to the standard output stream.
foreach(Raccoom.Xml.RssItem item in myRssChannel.Items)
{
    System.Console.WriteLine(item.Title);
}

This sample shows how to create and save RSS feeds to the local storage.

C#
Raccoom.Xml.RssChannel myRssChannel = new Raccoom.Xml.RssChannel();
myRssChannel.Title = "Sample rss feed";
myRssChannel.Copyright = "(c) 2003 by Christoph Richner";
// add item to channel
Raccoom.Xml.RssItem item = new Raccoom.Xml.RssItem();
item.Title = "Raccoom RSS 2.0 Framework announced";
item.Link = "http://jerrymaguire.sytes.net";
myRssChannel.Items.Add(item);

Save Feeds

This sample shows different ways to store the feed:

C#
// save feed to local storage
myRssChannel.Save(@"c:\cp.xml");
// upload feed to web server
myRssChannel.Publish(new Uri("http://www.mydomain.net"),
            "POST", "domain", "username", "password");
// create stream
System.IO.MemoryStream stream = new System.IO.MemoryStream();
myRssChannel.Write(stream);
stream.Close();

Transform Feeds

This sample shows how to consume and transform (XSLT/CSS) The Code Project feed:

C#
// consume RSS feed
Uri uri = new
  Uri("http://www.codeproject.com/webservices/articlerss.aspx?cat=1");
RssChannel myChannel = new RssChannel(uri);
// transform to stream
System.IO.MemoryStream memoryStream =
  myChannel.Transform(new System.Xml.XmlTextReader("transform.xslt"));
// transform to HTML output file
myChannel.Transform(new System.Xml.XmlTextReader("transform.xslt"),
                                                      "myChannel.htm");
// transform to HTML and XML output file
myChannel.Transform(new System.Xml.XmlTextReader("transform.xslt"),
                                        "channel.xml", "channel.htm");

Editors

Introduction

There is an MDI parent form which can hold any number of child forms. The child forms represent the document types, RSS and OPML. The editor provides a most recently used menu and common dialogs for save, open and publish operations. The built-in preview windows are docked using Magic Library and my Docking Manager Extender component which are provided in release mode assemblies.

Image 4

Type Converters

If you like to create well formatted documents which pass successfully the specification validators, you have to bear in mind that the input must also be valid. Here I present some type converters to edit kind of tricky values like RFC DateTime and ISO culture.

Image 5

IsoCodeUITypeConverter

Inherits from System.ComponentModel.TypeConverter and lets you choose your ISO Code from a list filled with System.Globalization.CultureTypes.AllCultures.

Image 6

RfcDateUITypeEditor

Inherits from System.Drawing.Design.UITypeEditor and makes it easy to select a date. It helps to format the DateTime to its RFC 822 value. DateTime natively supports RFC 822 DateTime format.

To format a DateTime instance as RFC 822 in your code, just type:

C#
string rfcDateFormat = myDateTime.ToString("r");

Image 7

SkipDayUITypeConverter

Inherits from System.Drawing.Design.UITypeEditor and makes it easy to edit values that are based on enumeration with FlagAttribute.

Drag'n'Drop Hyperlink from Browser to .NET Control

To allow drag'n'drop operations, you have to set AllowDrop to true, otherwise the DragXX messages are not sent. After enabling dropping, you have to consume DragEnter and DragDrop events.

We only allow dragging if the dragged data is UniformResourceLocator.

C#
/// <summary>
/// Allow dragging of UniformResourceLocator (Hyperlinks)
/// </summary>
private void OnDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
    if(e.Data.GetData("UniformResourceLocator",true)!=null)
    {
        e.Effect = DragDropEffects.Link;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }
}

Now that the user has released the left mouse button, we can get the UniformResourceLocator. It is important that you use a delegate to work on with the dropped hyperlink, otherwise the browser freezes until the event handler leaves, e.g. if you open a dialog that waits for user input.

C#
/// <summary>
/// If valid UniformResourceLocator was dropped, get it
/// </summary>
private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    string hyperLinkUrl = null;
    string hyperLinkText = null,
    //
    try
    {
        hyperLinkUrl = e.Data.GetData(typeof(string)) as string;
        // some browser deliver url and text
        // in UniformResourceLocator (Firebird)
        string[] tokens = hyperLinkUrl.Split('\\');
        if(tokens.Length>1)
        {
            hyperLinkUrl = tokens[0];
            hyperLinkText = tokens[1];
        }
        // we have to read FILEGROUPDESCRIPTOR to get the text (IE)
        else
        {
            System.IO.Stream ioStream=
            (System.IO.Stream)e.Data.GetData("FileGroupDescriptor");
            byte[] contents = new Byte[512];
            ioStream.Read(contents,0,512);
            ioStream.Close();
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //The magic number 76 is the size of that part of the
            //FILEGROUPDESCRIPTOR structure before
            // the filename starts - cribbed
            //from another usenet post.
            for (int i=76; contents[i] != 0; i++)
            {
                sb.Append((char)contents[i]);
            }
            if (!sb.ToString(sb.Length-4,4).ToLower().Equals(".url"))
            {
                throw new Exception("filename does not end in '.url'");
            }
            hyperLinkText = sb.ToString(0,sb.Length-4);
        }
        // do what ever you wanna do with the hyperlink
        this.BeginInvoke(.....)
    }
    catch (System.Exception ex)
    {
        System.Diagnostics.Trace.WriteLine(ex.Message);
    }
}

Conclusions

Feel free to use this framework in your application. Any feedback or criticism is appreciated.

Links

Blogs 

History 

  • 01.02.2004
    • Major update
  • 22.12.2003
    • RSS Editor enhanced
    • RssChannel.Transform() method added
  • 15.12.2003
    • Final release
  • Version 3.0.0.0 
    • Stable RFC Datetime parser 
    • IPersistentManager.Save method now provides an overloaded method taking an encoding parameter
    • Info: Visual Studio 2008 Project File Format

Have fun...

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
Switzerland Switzerland
My interest is in the future because I am going to spend the rest of my life there. (Charles Kettering)

Biography

  • 1996 - 1998 PC Board PPL, HTML, DHTML, Javascript and ASP
  • 1999 - 2001 coding Centura against Sql Database (SqlBase,MSSQL,Oracle)
  • 2002 - 2004 C# Windows Forms
  • 2005 - 2006 C# ASP.NET, Windows Forms
  • 2006 - 2009 C#, WCF, WF, WPF
  • 2010 - 2012 C#, Dynamics CRM, Sharepoint, Silverlight
  • 2013 - 2013 C#, WCF DS (OData), WF, WPF
  • 2014 - 2016 C#, Azure PaaS, Identity, OWIN, OData, Web Api
  • 2017 - now C#, aspnet.core, IdentityServer4, TypeScript & Angular @ Azure IaaS or PaaS

Interests

  • family & friends
  • chilaxing ,)
  • coding

Comments and Discussions

 
GeneralMy vote of 5 Pin
fredatcodeproject5-May-13 4:11
professionalfredatcodeproject5-May-13 4:11 
GeneralRe: My vote of 5 Pin
Chris Richner5-May-13 20:29
Chris Richner5-May-13 20:29 
QuestionSuper Excelent Piece of Work Pin
neoraltech14-Feb-13 16:03
neoraltech14-Feb-13 16:03 
AnswerRe: Super Excelent Piece of Work Pin
Chris Richner14-Feb-13 21:25
Chris Richner14-Feb-13 21:25 
QuestionWCF DataServices, OData http://www.odata.org/ Pin
Chris Richner19-Jan-13 22:45
Chris Richner19-Jan-13 22:45 
Generalthanks for sharing - have 5 Pin
Pranay Rana19-Jan-11 0:58
professionalPranay Rana19-Jan-11 0:58 
GeneralRe: thanks for sharing - have 5 Pin
Chris Richner19-Jan-11 2:41
Chris Richner19-Jan-11 2:41 
GeneralMy vote of 5 Pin
thatraja24-Nov-10 3:32
professionalthatraja24-Nov-10 3:32 
GeneralNice work... Pin
Sandeep Mewara14-May-10 6:07
mveSandeep Mewara14-May-10 6:07 
GeneralLINQifying Pin
[ICR]10-Oct-08 9:20
[ICR]10-Oct-08 9:20 
GeneralRe: LINQifying Pin
Chris Richner16-Sep-10 0:05
Chris Richner16-Sep-10 0:05 
GeneralSupport for RDF format Pin
ravensorb3-Oct-08 5:51
ravensorb3-Oct-08 5:51 
Questioni have to split th datetime from the mail body Pin
jayas18-Mar-08 19:41
jayas18-Mar-08 19:41 
GeneralRe: i have to split th datetime from the mail body Pin
Chris Richner25-Mar-08 9:52
Chris Richner25-Mar-08 9:52 
QuestionResponse time issue? Pin
sgub27-Jun-07 17:02
sgub27-Jun-07 17:02 
AnswerRe: Response time issue? Pin
Chris Richner27-Jun-07 19:04
Chris Richner27-Jun-07 19:04 
GeneralRe: Response time issue? Pin
Deveng8-Oct-07 6:33
Deveng8-Oct-07 6:33 
GeneralRe: Response time issue? Pin
Chris Richner8-Oct-07 7:50
Chris Richner8-Oct-07 7:50 
QuestionLicensing Pin
mark_in_skye4-Apr-07 5:49
mark_in_skye4-Apr-07 5:49 
AnswerRe: Licensing Pin
Chris Richner16-Sep-10 0:09
Chris Richner16-Sep-10 0:09 
GeneralPublishing Pin
Lord of Scripts18-Mar-07 3:37
Lord of Scripts18-Mar-07 3:37 
GeneralRe: Publishing Pin
Chris Richner9-Apr-07 12:01
Chris Richner9-Apr-07 12:01 
Hi DEGT,

What do you mean by "publish"?

myMsg.BehindDaKeys = "Chris Richner";

GeneralRe: Publishing Pin
Lord of Scripts9-Apr-07 21:01
Lord of Scripts9-Apr-07 21:01 
GeneralRe: Publishing Pin
Lord of Scripts9-Apr-07 21:06
Lord of Scripts9-Apr-07 21:06 
GeneralRe: Publishing Pin
Chris Richner10-Apr-07 1:35
Chris Richner10-Apr-07 1:35 

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.