No nodes selected from an Atom XML document using XPath?

I am trying to parse an Atom feed. I have XML-XML loaded as a string. I can load the XML in an XmlDocument . However, I cannot go through the document using XPath. Whenever I try, I get null .

I used this Atom feed as a test: http://steve-yegge.blogspot.com/feeds/posts/default

Calling SelectSingleNode() always returns null , unless I use " / ". Here is what I'm trying to do now:

 using (WebClient wc = new WebClient()) { string xml = wc.DownloadString("http://steve-yegge.blogspot.com/feeds/posts/default"); XmlNamespaceManager nsMngr = new XmlNamespaceManager(new NameTable()); nsMngr.AddNamespace(string.Empty, "http://www.w3.org/2005/Atom"); nsMngr.AddNamespace("app", "http://purl.org/atom/app#"); XmlDocument atom = new XmlDocument(); atom.LoadXml(xml); XmlNode node = atom.SelectSingleNode("//entry/link/app:edited", nsMngr); } 

I thought this was possible due to my XPath, so I also tried a simple root node query, as I knew that root should work:

 // I've tried both with & without the nsMngr declared above XmlNode node = atom.SelectSingleNode("/feed"); 

No matter what I do, it seems like he can't choose anything. Obviously, I'm missing something; I just can't figure that out. What do I need to do to get XPath to work in this Atom feed?

EDIT

Although this question has an answer, I found out that this question has an almost exact duplicate: stack overflow

+6
c # xml atom-feed xpath xmldocument
Feb 01 '09 at 17:13
source share
2 answers

While the C # implementation may allow default namespaces (I don't know), the XPath 1.0 specification does not work. So give Atom your own prefix:

 nsMngr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); 

And change your XPath accordingly:

 XmlNode node = atom.SelectSingleNode("//atom:entry/atom:link/app:edited", nsMngr); 
+8
Feb 01 '09 at 17:21
source share

Download the XML from the line and find all the Errors / Errors nodes.

 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlResult); XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable); nm.AddNamespace("ns", "http://somedomain.com/namespace1/2"); //ns - any name, make sure it is same in the below line XmlNodeList errors = xmlDoc.SelectNodes("/ns:*//ns:Errors/ns:Error", nm); 

-Matulan

0
Mar 22 '12 at 12:30
source share



All Articles