Syndication Library

Back in 2004 I created a syndication library for consuming and creating RSS/RDF/ATOM feeds. This library has been lying around for many years and I finally decided to make it public by making it downloadable from my homepage

This blog post will show some code examples and explain a little about how to use the library.

The first thing to show is how to create an RSS feed. I will not show how to create RDF or ATOM feeds because they follow the same procedure albeit other classes, and if you understand RSS you will understand the creation of RDF and ATOM as well. Note however that RSS/RDF and ATOM lie in their own namespaces.

In the following example, note that we use namespace extensions to the RSS feed using the SyndicationModule and SyndicationModuleCollection objects.

Rss rss = new Rss();
rss.Channel.Category.Add(new RssCategory(".NET"));
rss.Channel.Cloud = new RssCloud("www.ikjetil.no", "80", "path", "registerProcedure", "soap");
rss.Channel.Copyright = "Copyright (c) Kjetil Kristoffer Solberg";
rss.Channel.Description = "Description here.";
rss.Channel.Generator = "ItSoftware.Syndication";
rss.Channel.Language = "English";
rss.Channel.PubDate = RssDateTime.ToString(DateTime.Now, new TimeSpan(1, 0, 0));
rss.Channel.Title = "Showcase RSS feed.";
rss.Channel.Description = "RSS Channel Description.";
rss.Channel.Link = "http://www.itsoftware.no";

RssItem item = new RssItem();
rss.Channel.Items.Add(item);

item.Title = "A blog about a C# LINQ Provider";
item.PubDate = new RssDateTime(DateTime.Now, new TimeSpan(1, 0, 0)).ToString();
item.Link = "http://www.mylinkdomain.domain/rss.aspx?T=123";
item.Description = "I've just created a C# linq provider...";
item.Author = "Kjetil Kristoffer Solberg";
item.Category = new RssCategoryCollection();
item.Category.Add(new RssCategory(".NET"));
item.Category.Add(new RssCategory("C#"));
item.Category.Add(new RssCategory("LINQ"));
item.Comments = "http://www.mylinkdomain.domain/rss.aspx?T123#WhatDoYouThink";
item.GUID = new RssGuid(Guid.NewGuid().ToString(),"true");

item.Modules = new ItSoftware.Syndication.SyndicationModuleCollection();

SyndicationModule moduleCommentRss = item.Modules.Add( new SyndicationModule("wfw", "http://wellformedweb.org/CommentAPI/", SyndicationModuleNamespacePosition.DocumentElement));

SyndicationModuleElement moduleElementCommentRss = new SyndicationModuleElement("commentRss", "http://www.mydomain.domain/rss.aspx?T=123&C=1",
SyndicationModuleElementValueType.InnerText);
moduleCommentRss.Elements.Add(moduleElementCommentRss);

SyndicationModule moduleComments = item.Modules.Add( new SyndicationModule("slash", "http://purl.org/rss/1.0/modules/slash/", SyndicationModuleNamespacePosition.DocumentElement));
            
SyndicationModuleElement moduleElementComments = new SyndicationModuleElement("comments", "1", SyndicationModuleElementValueType.InnerText);
moduleComments.Elements.Add(moduleElementComments);

string rssXml = rss.Save(RssVersion.RSS_2_0_1, "utf-8").OuterXml;

To consume RSS/RDF and ATOM feeds you just do the following. Note that the uri could be a local file uri or http/ftp uri.

try
{
    SyndicationBase sb = Syndication.Load(new    Uri("http://www.ikjetil.no/Home/RssFeed/1?type=rss"));
                  
    if (sb is Rss)
    {
        // We have a RSS feed. Consume rss object.
        Rss rss = sb as Rss;
    }
    else if (sb is Rdf)
    {
        // We have a RDF feed. Consume rdf object.
        Rdf rdf = sb as Rdf;
    }
    else if (sb is Atom)
    {
        // We have an ATOM feed. Consume ATOM object.
        Atom atom = sb as Atom;
    }
}
catch (System.Net.WebException )
{
    // Invalid http/ftp uri
    throw;
}
catch ( SyndicationFormatInvalidException )
{
    // Invalid syndication format
    throw;
}

The next sample shows a simple F# console application that retrieves the current weather and prints it to the console

namespace WeatherHaugesund

open System
open System.Net
open ItSoftware.Syndication
open ItSoftware.Syndication.Rss

module ModuleMain =
 []
 let main(args : string[]) =
  let url = "http://www.yr.no/sted/Norge/Rogaland/Haugesund/Haugesund/varsel.rss"
  let sb : SyndicationBase = Syndication.Load(new Uri(url))
  let rss : Rss = sb :?> Rss
  printf "\n\n## Today ##\n%s\n\n" rss.Channel.Items.[0].Description
  0

These three examples should be all that you need to understand how to create and consume syndication feeds.

Note that you could also load the syndication feed from a string using the Syndication.LoadSyndication method.

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: