C# XmlDocument 选择节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1247398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# XmlDocument SelectNodes
提问by Daniel
I have an xml document with a root element, two child elements, 'diagnostic' and 'results'. The 'results' element then has an arbitrary number of elements with the name 'result'
我有一个 xml 文档,其中包含一个根元素、两个子元素、“诊断”和“结果”。'results' 元素包含任意数量的名为 'result' 的元素
When this is loaded into an XmlDocument it is easy to navigate the structure and see that this is exactly how things operate. I can write a recursive function that picks out all the "result" elements. The XmlDocument.SelectNodes("//results") finds a node no problem.
当它被加载到 XmlDocument 中时,很容易浏览结构并看到这正是事情的运作方式。我可以编写一个递归函数来挑选出所有“结果”元素。XmlDocument.SelectNodes("//results") 找到一个节点没有问题。
However,
* XmlDocument.SelectNodes("//results/result") finds nothing.
* XmlDocument.SelectNodes("//result") finds nothing.
但是, * XmlDocument.SelectNodes("//results/result") 什么也没找到。
* XmlDocument.SelectNodes("//result") 什么也没找到。
I've talked to a co-worker and he's had grief using Xpath in XmlDocument.SelectNodes. Anyone else run into this kind of problem? Any solutions?
我曾与一位同事交谈过,他对在 XmlDocument.SelectNodes 中使用 Xpath 感到悲痛。其他人遇到过这种问题吗?任何解决方案?
XML FILE:
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") produces no hits.
doc.SelectNodes("//result") 不产生任何命中。
采纳答案by Jon Skeet
Rob and Marc's answers are probably going in the right direction - XmlDocument + namespaces + XPath can be a bit of a pain.
Rob 和 Marc 的答案可能朝着正确的方向发展 - XmlDocument + 命名空间 + XPath 可能有点麻烦。
If you're able to use .NET 3.5, I suggest you use LINQ to XML instead. That would make it reallyeasy:
如果您能够使用 .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)
{
...
}
Basically LINQ to XML is a superior API in almost every way, in my experience :) (I believe there are some capabilities it's missing, but if you have access to .NET 3.5 it's definitely worth at least trying.)
根据我的经验,基本上 LINQ to XML 在几乎所有方面都是卓越的 API :)(我相信它缺少一些功能,但是如果您可以访问 .NET 3.5,那至少绝对值得尝试。)
回答by Marc Gravell
It sounds to me like namespaces are the issues; you generally need to enlist the help of an XmlNamespaceManager
for this, and use an alias in your queries, i.e.
在我看来,命名空间是问题所在;您通常需要为此寻求帮助XmlNamespaceManager
,并在查询中使用别名,即
doc.SelectNodes("//x:results/x:result", nsmgr);
(where x
is defined in nsmgr
as an alias to the given namespace)
(其中x
定义nsmgr
为给定命名空间的别名)