xml - SelectSingleNodeNS 中的命名空间参数需要什么格式

标签 xml windows-runtime

任何人都可以提供使用 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/

相关文章:

xmlns 声明被多个元素破坏, "global"范围

java - XSTREAM 数组转换器

xml - XSLT 对元素名称上的任何通用 XML 进行深度排序

c# - 如何验证签名的 XML 文件签名

c# - 单元测试应用程序(Windows 通用)中尚不存在 ExpectedException 属性?

android - 在 Android 中声明维度、颜色、资源 ID 或其他非标准数据类型的数组?

javascript - target 属性的 CSS 版本

windows - 在 WinRT XAML 中的 GridView 项目组内垂直滚动

deployment - 什么/为什么 WinRT 随机创建新的干净布局

javascript - 使用共享合约使用图像文件