views:

116

answers:

2

I'm trying to use XDocument and XPathEvaluate to get values from the woot.com feed. I'm handling other namespaces fine, but this example is giving me problems.

     <rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
      <channel>    
        <category text="Comedy" xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd"&gt;
</category> 
<!-- this is a problem node, notice 'xmlns=' --!>

So I try this:

  XmlNamespaceManager man = new XmlNamespaceManager(nt);
    man.AddNamespace("ns", "http://www.w3.org/2000/xmlns/");
// i've also tried man.AddNamespace("ns", string.Empty);
    xDocument.Namespace = man;
    var val = xDocument.XPathEvaluate("/rss/channel/ns:category/@text", xDocument.Namespace);

val is always null. I'm using ns: from the suggestion from VS 2010 XPath Navigator plugin. Any thoughts on how to handle this?

+1  A: 

The element category is in namespace http://www.itunes.com/dtds/podcast-1.0.dtd. It's not an empty namespace. It just isn't given a prefix in input XML.

man.AddNamespace("ns", "http://www.itunes.com/dtds/podcast-1.0.dtd");
...
xDocument.XPathEvaluate("/rss/channel/ns:category/@text", xDocument.Namespace);
Pavel Minaev
+1  A: 
man.AddNamespace("ns", "http://www.w3.org/2000/xmlns/");

Here is the error: you bind to the wrong namespace.

Must be:

man.AddNamespace("ns", "http://www.itunes.com/dtds/podcast-1.0.dtd"); 
Dimitre Novatchev