![]() |
License: The Code Project Open License (CPOL)
Reading Feeds with XLINQBy Sacha BarberI 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
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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
![]()
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.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
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 |