Ok, last night I was trying to throw together an RSS parser. On my first feed, everything worked fine, but when I tried it against slashdot's rss feed, everything went to crap. Dim
Here's the slashdot feed for reference:
http://rss.slashdot.org/Slashdot/slashdot
first I tried:
xmlDoc.SelectNodes("/RDF/item")
but that threw an exception. I looked it up and found out I needed to account for the namespaces with a XmlNamespaceManager, so then I ended up with this code:
nsManager.AddNamespace("", "http://purl.org/rss/1.0/")
nsManager.AddNamespace("rdf", http://www.w3.org/1999/02/22-rdf-syntax-ns#")nsManager.AddNamespace("slash">
http://www.w3.org/1999/02/22-rdf-syntax-ns#")
nsManager.AddNamespace("slash", "http://purl.org/rss/1.0/modules/slash/")
nsManager.AddNamespace("taxo", "http://purl.org/rss/1.0/modules/taxonomy/")
nsManager.AddNamespace("dc", "http://purl.org/dc/elements/1.1/")
nsManager.AddNamespace("syn", "http://purl.org/rss/1.0/modules/syndication/")
nsManager.AddNamespace("admin", "http://webns.net/mvcb/")
nsManager.AddNamespace("feedburner", "http://rssnamespace.org/feedburner/ext/1.0")
itemNodes = xmlDoc.SelectNodes("/rdf:RDF")
That code returns all the body elements. Great! Then I try
itemNodes = xmlDoc.SelectNodes("/rdf:RDF/item") which should select all the items, but it doesn't!! I tried several different things but i couldnt seem to get xpath to give me the child items of the rdf:RDF node.
Anyone know what i'm doing wrong there?
thanks
-
-
itemNodes = xmlDoc.documentElement.SelectNodes("item")
-
Well;
As related refrences read this post from Scott Hanselman and my article on code project about XMLDataSource in ASP.NET 2.0
-
themaffeo wrote:nsManager.AddNamespace("", "http://purl.org/rss/1.0/")
That is something you cannot do. It is one of the limitations of XPath 1.0 (to be fixed in XPath 2.0) that you cannot change the default namespace. If you want to select items that are in a namespace, you must use a prefix. So just assign any prefix you want to that namespace (remember, the prefixes you use don't need to match the source document).
nsManager.AddNamespace("rss", "http://purl.org/rss/1.0/")
nsManager.AddNamespace("rdf", http://www.w3.org/1999/02/22-rdf-syntax-ns#")
itemNodes = xmlDoc.selectNodes("/rdf:RDF/rss:item", nsManager)
That should work.
-
Thanks all for your help - BTW, Sven: your solution worked prefectly
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.