【问题标题】:C# XmlDocument SelectNodesC# XmlDocument 选择节点
【发布时间】:2009-08-07 23:07:39
【问题描述】:

我有一个带有根元素、两个子元素“诊断”和“结果”的 xml 文档。 'results' 元素有任意数量的名为 'result' 的元素

当它被加载到 XmlDocument 中时,很容易导航结构并看到这正是事情的运作方式。我可以编写一个递归函数来挑选所有“结果”元素。 XmlDocument.SelectNodes("//results") 发现一个节点没问题。

然而, * XmlDocument.SelectNodes("//results/result") 什么也没找到。
* XmlDocument.SelectNodes("//result") 什么也没找到。

我与一位同事交谈过,他对在 XmlDocument.SelectNodes 中使用 Xpath 感到很痛苦。还有其他人遇到这种问题吗?有什么解决办法吗?

XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="10" yahoo:created="2009-08-07T10:19:59Z" yahoo:lang="en-US" yahoo:updated="2009-08-07T10:19:59Z" yahoo:uri="http://query.yahooapis.com/v1/yql?q=select+*+from+search.news+where+query%3D%22Tanzania%22">
    <diagnostics>
        <publiclyCallable>true</publiclyCallable>
        <url execution-time="47"><![CDATA[http://boss.yahooapis.com/ysearch/news/v1/Tanzania?format=xml&start=0&count=10]]></url>
        <user-time>49</user-time>
        <service-time>47</service-time>
        <build-version>2579</build-version>
    </diagnostics>
    <results>
        <result xmlns="http://www.inktomi.com/">
            <abstract>Kakungulu Cup winners SC Villa face Tanzania’s Simba SC this afternoon at the National stadium in Dar es salaam. “We had a very tiresome journey. The road was so bad and the road blocks were so many. However, we finally reached but the boys were so tired,” said Kato.</abstract>
            <clickurl>http://lrd.yahooapis.com/_ylc=X3oDMTQ4cXAxcnRoBF9TAzIwMjMxNTI3MDIEYXBwaWQDb0pfTWdwbklrWW5CMWhTZnFUZEd5TkouTXNxZlNMQmkEY2xpZW50A2Jvc3MEc2VydmljZQNCT1NTBHNsawN0aXRsZQRzcmNwdmlkA21VVGlta2dlQXUzeEYuM0xGQkQzR1pUU1FIS0dORXA4cUk4QUJJX1U-/SIG=12vhpskdd/**http%3A//www.monitor.co.ug/artman/publish/sports/SC_Villa_face_Simba_in_Tanzania_89289.shtml</clickurl>
            <date>2009/08/07</date>
            <language>english</language>
            <source>The Monitor</source>
            <sourceurl>http://www.monitor.co.ug/</sourceurl>
            <time>20:22:32</time>
            <title>SC Villa face Simba in Tanzania</title>
            <url>http://www.monitor.co.ug/artman/publish/sports/SC_Villa_face_Simba_in_Tanzania_89289.shtml</url>
        </result>

XPATH

doc.SelectNodes("//result") 不产生任何命中。

【问题讨论】:

  • 你有相关xml的样本吗?您的“结果”元素是否可能实际上与“结果”元素位于不同的命名空间中?元素上是否有任何限定前缀?

标签: c# xml xpath


【解决方案1】:

Rob 和 Marc 的答案可能朝着正确的方向发展 - XmlDocument + 命名空间 + XPath 可能有点麻烦。

如果您能够使用 .NET 3.5,我建议您改用 LINQ to XML。这会让它真的变得容易:

XDocument doc = XDocument.Load("foo.xml");
XNamespace ns = "bar";
var results = doc.Descendants(ns + "result");

foreach (var result in results)
{
    ...
}

根据我的经验,基本上 LINQ to XML 在几乎所有方面都是一个出色的 API :)(我相信它缺少一些功能,但如果您可以访问 .NET 3.5,那么至少值得一试。)

【讨论】:

  • 感谢乔恩,我确实切换到了 LINQ,而且它好多了! :)
  • 这个是为我做的,但我的问题是 XNamespace 部分。我讨厌给 Jon Skeet 更多分数……但是,……长尾巴等等。
  • @John:如果有什么安慰的话,你的支持并没有给我更多的代表。大约 9 小时前,我达到了代表上限。
  • @Jon 大约在 00:01,我想 :) 我希望我有那些代表问题 :D
【解决方案2】:

在我看来,命名空间是问题所在;您通常需要为此寻求XmlNamespaceManager 的帮助,并在您的查询中使用别名,即

doc.SelectNodes("//x:results/x:result", nsmgr);

(其中xnsmgr 中定义为给定命名空间的别名)

【讨论】:

  • 谢谢马克,就是这样。
猜你喜欢
  • 2015-10-22
  • 2017-05-01
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
  • 2020-05-30
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
相关资源
最近更新 更多