开发者

How can I handle an empty namespace with XDocument.XPathEvaluate?

开发者 https://www.devze.com 2023-01-04 04:59 出处:网络
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.

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">
</category> 
<!-- this is a problem node, n开发者_Go百科otice '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?


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);


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"); 
0

精彩评论

暂无评论...
验证码 换一张
取 消