带有 XmlDocument 的 XPath 找不到节点 [英] XPath with XmlDocument not finding nodes

查看:49
本文介绍了带有 XmlDocument 的 XPath 找不到节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的 XPath 无法从 XML 树中选择任何节点时遇到了一些问题.到目前为止,这是我的代码:

so I'm having some trouble with my XPath not selecting any nodes from an XML tree. Here's my code so far:

var reader = new XmlDocument();
reader.Load(@"http://www.fieldgulls.com/rss/current");

XmlNodeList list = reader.SelectNodes("./entry");

我还尝试了 */entry、//entry 等的 XPath 值.我似乎无法得到任何工作.我做错了什么?

I've also tried XPath values of */entry, //entry, and others. I can't seem to get anything to work though. What am I doing wrong?

推荐答案

问题是元素其实是在根节点的默认命名空间中,也就是http://www.fieldgulls.com/rss/current":

The problem is that the elements <Entry> are actually in the default namespace of the root node, which is "http://www.fieldgulls.com/rss/current":

<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"> <!--The default namespace for nested elements is set here with the xmlns= attribute -->
  <title>Field Gulls -  All Posts</title>
  <subtitle>The stupidest name in smart football analysis.</subtitle>
  <icon>https://cdn3.vox-cdn.com/community_logos/50215/fieldgulls-fav.png</icon>
  <updated>2016-11-13T17:00:02-08:00</updated>
  <id>http://www.fieldgulls.com/rss/current/</id>
  <link type="text/html" href="http://www.fieldgulls.com/" rel="alternate"/>
  <entry>
    <!--Remainder commented out-->

因此您需要使用适当的命名空间和适当的SelectNodes() 覆盖:

Thus you need to select nodes using the appropriate namespace and the appropriate SelectNodes() override:

var reader = new XmlDocument();
reader.Load(@"http://www.fieldgulls.com/rss/current");

var nsmgr = new XmlNamespaceManager(reader.NameTable);
nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");

XmlNodeList list = reader.SelectNodes(".//a:entry", nsmgr);

在这种情况下,我发现使用以下基于更新的调试实用程序会很有帮助 LINQ to XML 类库,使每个节点的命名空间显而易见:

In cases like this, I find it helpful to use the following debugging utility, based on the newer LINQ to XML class library, to make the namespace of each node apparent:

public static class XObjectExtensions
{
    public static IEnumerable<string> DumpXmlElementNames(this XDocument doc)
    {
        return doc.Root.DumpXmlElementNames();
    }

    public static IEnumerable<string> DumpXmlElementNames(this XElement root)
    {
        if (root == null)
            return Enumerable.Empty<string>();
        var startCount = root.AncestorsAndSelf().Count();
        return root.DescendantsAndSelf().Select(el => string.Format("{0}\"{1}\"",
            new string(' ', 2 * (el.AncestorsAndSelf().Count() - startCount)), el.Name.ToString()));
    }
}

然后,在调试时,你会这样做:

Then, when debugging, you would do:

Console.WriteLine("Dumping a list of all element names and namespaces: ");
Console.WriteLine(String.Join("\n", XDocument.Load(@"http://www.fieldgulls.com/rss/current").DumpXmlElementNames()));

产生以以下开头的输出:

Which produces an output that starts with:

"{http://www.w3.org/2005/Atom}feed"
  "{http://www.w3.org/2005/Atom}title"
  "{http://www.w3.org/2005/Atom}subtitle"
  "{http://www.w3.org/2005/Atom}icon"
  "{http://www.w3.org/2005/Atom}updated"
  "{http://www.w3.org/2005/Atom}id"
  "{http://www.w3.org/2005/Atom}link"
  "{http://www.w3.org/2005/Atom}entry"

示例 fiddle.

这篇关于带有 XmlDocument 的 XPath 找不到节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆