任何人都可以提供使用 XmlDocument.SelectSingleNodeNS 的示例吗? WinRT 的功能?我不清楚第二个参数需要什么,也找不到示例。
public IXmlNode SelectSingleNodeNS(
string xpath,
object namespaces
)
Contains a string that specifies the namespaces to use in XPath expressions when it is necessary to define new namespaces externally. Namespaces are defined in the XML style, as a space-separated list of namespace declaration attributes. You can use this property to set the default namespace as well.
最佳答案
namespaces 参数显然只是一个字符串(尽管声明为对象),它必须包含格式为“xmlns:aliasname='namespace'”(XML 样式)的 XML 命名空间声明。例如
xmlDocument.DocumentElement.SelectNodesNS("cb:person",
"xmlns:cb='http://www.addison-wesley.de/codebook'");
像这样处理 XML 文档:
<?xml version="1.0" encoding="utf-8" ?>
<persons xmlns="http://www.addison-wesley.de/codebook">
<person id="1000">
<firstname>Zaphod</firstname>
<lastname>Beeblebrox</lastname>
<type>Alien</type>
</person>
...
</persons>
请注意,XPath 中使用了别名 (cb:)。
如果命名空间不是 XML 样式,您将收到臭名昭著的 COM 错误 E_Fail。
SelectNodesNS 的(糟糕的)文档说: "包含一个字符串,当需要在外部定义新的命名空间时,指定在 XPath 表达式中使用的命名空间。命名空间以 XML 样式定义,作为以空格分隔的命名空间声明属性列表。您可以使用此属性设置默认值命名空间也是如此。”
根据该 namespace 必须是一个字符串,并且可以包含多个 XML namespace (尚未尝试)。问题仍然悬而未决,为什么它是一个对象。
关于xml - SelectSingleNodeNS 中的命名空间参数需要什么格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13325541/