SelectNodes not working on stackoverflow feed
Asked Answered
P

4

16

I'm trying to add support for stackoverflow feeds in my rss reader but SelectNodes and SelectSingleNode have no effect. This is probably something to do with ATOM and xml namespaces that I just don't understand yet.

I have gotten it to work by removing all attributes from the feed tag, but that's a hack and I would like to do it properly. So, how do you use SelectNodes with atom feeds?

Here's a snippet of the feed.

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:thr="http://purl.org/syndication/thread/1.0">

<title type="html">StackOverflow.com - Questions tagged: c</title>
<link rel="self" href="http://stackoverflow.com/feeds/tag/c" type="application/atom+xml" />
<subtitle>Check out the latest from StackOverflow.com</subtitle>
<updated>2008-08-24T12:25:30Z</updated>
<id>http://stackoverflow.com/feeds/tag/c</id>
<creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license>


 <entry>
   <id>https://mcmap.net/q/169232/-what-is-the-best-way-to-communicate-with-a-mysql-server</id>
   <title type="html">What is the best way to communicate with a SQL server?</title>
   <category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="c" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="c++" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="sql" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="mysql" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="database" />  
   <author><name>Ed</name></author>
   <link rel="alternate" href="https://mcmap.net/q/169232/-what-is-the-best-way-to-communicate-with-a-mysql-server" />
   <published>2008-08-22T05:09:04Z</published>
   <updated>2008-08-23T04:52:39Z</updated>
   <summary type="html">&lt;p&gt;I am going to be using c/c++, and would like to know the best way to talk to a MySQL server.  Should I use the library that comes with the server installation?  Are they any good libraries I should consider other than the official one?&lt;/p&gt;</summary>
   <link rel="replies" type="application/atom+xml" href="http://stackoverflow.com/feeds/question/22901/answers" thr:count="2"/>
   <thr:total>2</thr:total>
 </entry>


</feed>


The Solution

XmlDocument doc = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
doc.Load(feed);

// successful
XmlNodeList itemList = doc.DocumentElement.SelectNodes("atom:entry", nsmgr);
Petuntse answered 24/8, 2008 at 0:40 Comment(1)
Does -> nsmgr.AddNamespace("atom", "w3.org/2005/Atom"); <- actually connect over the internet to that url to get the schema? if so, what happens if it can't connect to that url?Kra
A
9

Don't confuse the namespace names in the XML file with the namespace names for your namespace manager. They're both shortcuts, and they don't necessarily have to match.

So you can register "http://www.w3.org/2005/Atom" as "atom", and then do a SelectNodes for "atom:entry".

Anesthesia answered 24/8, 2008 at 1:3 Comment(0)
T
6

You might need to add a XmlNamespaceManager.

XmlDocument document = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("creativeCommons", "http://backend.userland.com/creativeCommonsRssModule");
// AddNamespace for other namespaces too.
document.Load(feed);

It is needed if you want to call SelectNodes on a document that uses them. What error are you seeing?

Truthfunction answered 24/8, 2008 at 0:45 Comment(0)
A
2

You've guessed correctly: you're asking for nodes not in a namespace, but these nodes are in a namespace.

Description of the problem and solution: http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx

Anesthesia answered 24/8, 2008 at 0:43 Comment(0)
P
0

I just want to use..

XmlNodeList itemList = xmlDoc.DocumentElement.SelectNodes("entry");

but, what namespace do the entry tags fall under? I would assume xmlns="http://www.w3.org/2005/Atom", but it has no title so how would I add that namespace?

XmlDocument document = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("", "http://www.w3.org/2005/Atom");
document.Load(feed);

Something like that?

Petuntse answered 24/8, 2008 at 1:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.