C# XmlNode.SelectNodes 的基础知识?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/699184/
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
Basics of XmlNode.SelectNodes?
提问by Yes - that Jake.
I'm not sure why this isn't working.
我不确定为什么这不起作用。
I have an XmlNode in a known-format. It is:
我有一个已知格式的 XmlNode。这是:
<[setting-name]>
<dictionary>
<[block-of-xml-to-process]/>
<[block-of-xml-to-process]/>
<[block-of-xml-to-process]/>
</dictionary>
</[setting-name]>
I have a reference to the node in a variable called pattern. I want an iterable collection of nodes, each of which is represented by a [block-of-xml-to-process] above. The name and structure of the blocks is unknown at this point. [Setting-name] is known.
我在名为pattern的变量中有对节点的引用。我想要一个可迭代的节点集合,每个节点都由上面的 [block-of-xml-to-process] 表示。此时块的名称和结构未知。[设置名称] 已知。
This seems pretty straightforward. I can think of a half-dozen XPATH expressions that should point to the blocks. I've tried:
这看起来很简单。我可以想到六个应该指向块的 XPATH 表达式。我试过了:
XmlNodeList kvpsList = pattern.SelectNodes(String.Format(@"/{0}/dictionary/*", _CollectionName));
XmlNodeList kvpsList = pattern.SelectNodes(String.Format(@"{0}/dictionary/*", _CollectionName));
XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary/*");
XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary");
But, I am apparently lacking some basic understanding of XPATH or some special trick of .SelectNodes because none of them work consistently.
但是,我显然对 XPATH 或 .SelectNodes 的一些特殊技巧缺乏一些基本的了解,因为它们都无法始终如一地工作。
What am I doing wrong?
我究竟做错了什么?
回答by shahkalpesh
What is the use of variable pattern
?
Is it a reference to the DOM of the entire XML?
变量有pattern
什么用?
它是对整个 XML 的 DOM 的引用吗?
See what this results into
pattern.SelectNodes("//dictionary/").ChildNodes.Count
看看这会产生什么结果
pattern.SelectNodes("//dictionary/").ChildNodes.Count
EDIT: Is this well-formed xml?
编辑:这是格式良好的 xml 吗?
回答by David
Could namespaces be causing a problem? Also, try looking at "pattern.OuterXml" to make sure you are looking at the correct element.
命名空间会导致问题吗?此外,尝试查看“pattern.OuterXml”以确保您查看的是正确的元素。
回答by dkarzon
Have you tried:
你有没有尝试过:
XmlNodeList kvpsList = pattern.SelectNodes(@"//dictionary:child");
OR
或者
XmlNodeList kvpsList = pattern.SelectNodes(@"/[setting-name]/dictionary:child");
Pretty much gets the children of "dictionary" If that doesnt work, does the actual call to dictionary work?
几乎可以得到“字典”的孩子 如果这不起作用,实际调用字典是否有效?
回答by marc_s
Have you tried removing the "@" from your XPath strings??
您是否尝试从 XPath 字符串中删除“@”?
XmlNodeList kvpsList = pattern.SelectNodes("//dictionary");
That should work - does work for me on a daily basis :-)
那应该有效 - 每天都对我有用:-)
Marc
马克
回答by Knots
I was just searching for this and found that it worked if you just type:
我只是在寻找这个,发现如果你只输入它就可以工作:
XmlNodeList kvpsList = pattern.SelectNodes("dictionary");
kvpsList will then contain all [block-of-xml-to-process]-s, though I cannot see why. =')
kvpsList 然后将包含所有 [block-of-xml-to-process]-s,但我不明白为什么。=')
回答by Triynko
I encountered the same problem, and it seems to be a known, but unexpected behavior. See Xml-SelectNodes with default-namespace via XmlNamespaceManager not working as expected
我遇到了同样的问题,这似乎是一个已知但意外的行为。通过 XmlNamespaceManager查看具有默认命名空间的 Xml-SelectNodes 未按预期工作
For example, I got it to work by instantiating a XmlNamespaceManager using the XmlDocument's NameTable, then added a namespace with an arbitrary name such as "a" associated with the NamespaceURI of the main document element. Note that the XmlDocument's NamespaceURI was blank in my case, but its DocumentElement's NameSpaceURI actually had a value. That's probably why it wouldn't work without specifying a namespace originally.
例如,我通过使用 XmlDocument 的 NameTable 实例化 XmlNamespaceManager 来使其工作,然后添加一个具有任意名称的命名空间,例如与主文档元素的 NamespaceURI 关联的“a”。请注意,在我的例子中,XmlDocument 的 NamespaceURI 是空白的,但它的 DocumentElement 的 NameSpaceURI 实际上有一个值。这可能就是为什么最初不指定命名空间就无法工作的原因。
XmlDocument doc = new XmlDocument();
doc.Load( file.FullName );
XmlNode docElement = doc.DocumentElement as XmlNode;
XmlNamespaceManager nsman = new XmlNamespaceManager( doc.NameTable );
nsman.AddNamespace( "a", docElement.NamespaceURI );
docElement.SelectNodes( "a:wavetrack", nsman ); //docElement.SelectNodes("wavetrack") wasn't working