Skip to content

Instantly share code, notes, and snippets.

@NickJosevski
Created January 15, 2012 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NickJosevski/1614853 to your computer and use it in GitHub Desktop.
Save NickJosevski/1614853 to your computer and use it in GitHub Desktop.
Doesn't work for funnelweb blogs, but works fine on feedburner feed and Wordpress feed.
//just paste this code in LinqPad (having selected Language: C# Program) and try and fix it ;)
void Main()
{
//demonstrating linq to xml handing of namespaces, using the feedburner style on Scott Hanselman's blog.
var request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(@"http://feeds.feedburner.com/ScottHanselman");
//this works:
using (var response = request.GetResponse())
{
var xmlDoc = XDocument.Load(response.GetResponseStream());
var ns = XNamespace.Get(@"http://rssnamespace.org/feedburner/ext/1.0");
var elems = from x in xmlDoc.Descendants(XName.Get("item"))
select x.Element(XName.Get("origLink", ns.NamespaceName)).Value;
elems.ToList().ForEach(Console.WriteLine);
}
Console.WriteLine("now try another type of feed...");
//this doesn't work. Something to do with the different structure of FunnelWeb RSS feed, I'm out of ideas
request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(@"http://www.paulstovell.com/feed");
using (var response = request.GetResponse())
{
System.Xml.Linq.XNamespace ns = "http://www.w3.org/2005/Atom";
var xmlDoc = XDocument.Load(response.GetResponseStream());
// Thanks to this answer on SO, it is now udpated and works as intended
// http://stackoverflow.com/a/8876382/75963
var elems = from x in xmlDoc.Descendants(ns + "entry")
select x.Element(ns + "id").Value;
elems.ToList().ForEach(Console.WriteLine);
if(!elems.Any())
Console.WriteLine("failed to read the 2nd feed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment