微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – SelectSingleNodeNS中namespaces参数的预期格式

任何人都可以提供使用WinFT XmlDocument.SelectSingleNodeNS功能的示例吗?我不清楚第二个参数是什么期待的,我找不到一个例子.

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>

请注意,别名(cb :)用于XPath.

如果命名空间不是XML样式,则会出现臭名昭着的COM错误E_Fail.

SelectNodesNS的(差)文档说:
“当需要在外部定义新的命名空间时,包含指定用于XPath表达式的命名空间的字符串.命名空间以XML样式定义,作为空格分隔的命名空间声明属性列表.您可以使用此属性设置认值命名空间.“

根据命名空间必须是一个字符串,可能包含多个XML命名空间(尚未尝试).问题仍然是公开为什么它是一个对象.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。