Click here to Skip to main content
6,630,289 members and growing! (21,578 online)
Email Password   helpLost your password?
License: The Code Project Open License (CPOL)

Reading Feeds with XLINQ

By Sacha Barber

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: [...]
All Topics
Version:2 (See All)
Posted:17 Jun 2009
Views:1,447
Bookmarked:4 times
Technical Blog
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
1 vote for this technical blog.
Popularity: 0.00 Rating: 5.00 out of 5

1

2

3

4
1 vote, 100.0%
5
A Technical Blog article. View entire blog here.

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:  }

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:  }

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-thumb19.png

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sacha Barber


Member
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Occupation: Software Developer (Senior)
Location: United Kingdom United Kingdom

Other popular Uncategorised Technical Blogs articles:

  • Multi-Threading in ASP.NET
    ASP.Net Threading Inside the ASP.Net Worker Process there are two thread pools. Theworker thread pool handles all incoming requests and the I/O Threadpool handles the I/O (accessing the file system, web services anddatabases, etc.). Each App Domain has its own thread pool and thenumber of ope
  • Delegates Explained in Plain English
    CodeProjectDelegates are fundamental to the .NET Framework (events and callbacks wouldn't work without them) and can be extremely powerful to the .NET Developer once they come to grasps with exactly what they are and how to use them. In this blog I will consider aspects of a real world situation in
  • Windows 7 Tricks and Keyboard Shortcuts
    I’ve been running Windows 7 RC for a little over a week now and can’t imagine going back to Vista at this point. I decided to start with a fresh install of Windows 7, so I’ve been in the process of reinstalling all of my applications and cleaning up my disk drives. In the process, I went searching
  • iPhone Gaming Framework: Stage 1 Tutorial
    The goal for this tutorial is to get a basic screen management system up and running, ready to start writing game code.
  • Thread Safe Generic Queue Class
    I've been doing a lot of mult-threading work, recently, using the standard Thead class, the Worker Queue, and the new PLINQ (Parallel LINQ). The problem with most of the built-in generic collections (Queue, List, Dictionary, etc), is that they are not thread safe.I created a library of
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralGreat PinmemberDaniel Vaughan5:37 17 Jun '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jun 2009
Editor: Sean Ewington
Copyright 2009 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project