Reading Feeds with XLINQ

I have done a number of projects that all parse RSS Feeds using XLINQ. Which in itself is awesome, and much easier than using old XML techniques. Where I would so something like

   1:  public static List<PhotoInfo> LoadLatestPictures()
   2:  {
   3:   try
   4:   {
   5:     var xraw = XElement.Load(MOST_RECENT);
   6:     var xroot = XElement.Parse(xraw.ToString());
   7:     var photos = (from photo in xroot.Element("photos").
   8:     Elements("photo")
   9:      select new PhotoInfo
  10:      {
  11:        ImageUrl =
  12:        string.Format(
  13:        "http://farm{0}.static.flickr.com/{1}/{2}_{3}_m.jpg",
  14:        (string)photo.Attribute("farm"),
  15:        (string)photo.Attribute("server"),
  16:        (string)photo.Attribute("id"),
  17:        (string)photo.Attribute("secret"))
  18:      }).Take(Constants.ROWS * Constants.COLUMNS);
  19:              return photos.ToList<PhotoInfo>();
  20:    }
  21:    catch (Exception e)
  22:    {
  23:      Trace.WriteLine(e.Message, "ERROR");
  24:    }
  25:    return null;
  26:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }However today I found something even cooler.

Have a look at the code below.

   1:  using System.ServiceModel.Syndication;
   2:  using System.Xml;
   3:  using System.IO;
   4:  using System.Xml.Linq;
   5:  
   6:  public class DataService : IDataService
   7:  {
   8:      public SyndicationFeedFormatter GetGeoRSS()
   9:      {
  10:          var geoRss =
  11:          @"<?xml version='1.0' encoding='utf-8' ?>
  12:              <rss version='2.0'
  13:                  xmlns:geo='http://www.w3.org/2003/01/geo/wgs84_pos#'
  14:                  xmlns:georss='http://www.georss.org/georss'
  15:                  xmlns:gml='http://www.opengis.net/gml'
  16:                  xmlns:mappoint='http://virtualearth.msn.com/apis/annotate#'>
  17:                <channel>
  18:                  <title>Mount Saint Helens - Mount Margaret Trail</title>
  19:                  <link></link>
  20:                  <description>Trailheads and campsites in the Mount
  21:                  Margaret area of
  22:                  Mount Saint Helens, WA</description>
  23:                  <mappointIntlCode>cht</mappointIntlCode>
  24:                  <item>
  25:                    <title>Coldwater Lake</title>
  26:                    <description>Formed by the 1980 eruption of
  27:                      Mount St. Helens.</description>
  28:                    <georss:polygon>46.31409 -122.22616 46.31113
  29:                      -122.22968 46.31083 -122.23320 46.29802
  30:                      -122.25877 46.29245 -122.26641 46.29286 -122.26392
  31:                      46.28746 -122.26744 46.28741 -122.26006
  32:                      46.29049 -122.25955 46.29120 -122.25620
  33:                      46.28924 -122.255430 46.30271 -122.23251
  34:                      46.31284 -122.22315
  35:                      46.31409 -122.22616</georss:polygon>
  36:                    <icon>http://dev.live.com/virtualearth/sdk/
  37:                      img/hiking_icon.gif</icon>
  38:                  </item>
  39:                  <item>
  40:                    <title>Lakes Trailhead</title>
  41:                    <description>This is where we started our hike,
  42:                      just down the road from the visitor center.
  43:                      You could also start at the visitor center.
  44:                  </description>
  45:                    <geo:lat>46.2913246</geo:lat>
  46:                    <geo:long>-122.2658157</geo:long>
  47:                  </item>
  48:                </channel>
  49:              </rss>";
  50:  
  51:          var xDoc = XDocument.Parse(geoRss);
  52:          var feed = SyndicationFeed.Load(xDoc.CreateReader());
  53:  
  54:          Rss20FeedFormatter feed2 = Rss20FeedFormatter(feed);
  55:          return feed2;
  56:      }
  57:  }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

The thing to note here is the “SyndicationFeed” and “Rss20FeedFormatter” classes. How cool is that.

So when you a  Rss20FeedFormatter object, you get properties to get straight to all the usual RSS things you would like to use, such as Feed.Items

image

This is very cool. I am constantly being surprised by just what .NET can do, it just goes to show you need to keep an eye on the namespaces. There are new ones in there all the time.

System.ServiceModel.Syndication Huh, well I’ll be.

Advertisements

6 thoughts on “Reading Feeds with XLINQ

  1. Josh Smith says:

    I smell Times Reader all over those classes!

  2. sacha barber says:

    actually the raw XLINQ stuff was from Marsa, and the RSS find was from a Virtual Earth API demo

  3. Mathew Thomas says:

    Do you have a full code example you could share? Thanks!

  4. sacha says:

    Sure you can get the stuff I noticed this in first, from here
    http://blogs.msdn.com/publicsector/archive/2008/05/01/updated-virtual-earth-map-control-demo-code.aspx

    which is a Virtual Earth demo, just download that and search the VS2008 solution for “Rss20FeedFormatter”, that should give you the project where its used.

  5. tip says:

    This is such a wonderful and informative way to reach others. I Will be more than glad to share this site

  6. auto insurance discounters says:

    Hello, I’m a regular visitor to your site so i finally decided its time to sign your guestbook, so here i sign !

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: