Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

Reading Feeds with XLINQ

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
17 Jun 2009CPOL 17.6K   8   1
How to read 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:

C#
 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:

C#
 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.

37337/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)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
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 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • 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

Comments and Discussions

 
GeneralGreat Pin
Daniel Vaughan17-Jun-09 4:37
Daniel Vaughan17-Jun-09 4:37 

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.