Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell、xml和名称空间_Xml_Powershell_Xpath - Fatal编程技术网

powershell、xml和名称空间

powershell、xml和名称空间,xml,powershell,xpath,Xml,Powershell,Xpath,我有一些XML: 连接 路由器已连接 无法处理它,因为名称空间。尝试了很多,但没有成功 [string]$xpath='/SOAP:Envelope/SOAP:Body/P2MessageServiceStatus/@CONNECTION' $wc=新对象Net.WebClient [xml]$stuff=$wc.DownloadString($url) $ns=新对象Xml.XmlNamespaceManager$stuff.NameTable $ns.AddNamespace(“SOAP

我有一些XML:


连接
路由器已连接
无法处理它,因为名称空间。尝试了很多,但没有成功

[string]$xpath='/SOAP:Envelope/SOAP:Body/P2MessageServiceStatus/@CONNECTION'
$wc=新对象Net.WebClient
[xml]$stuff=$wc.DownloadString($url)
$ns=新对象Xml.XmlNamespaceManager$stuff.NameTable
$ns.AddNamespace(“SOAP”,$xmlns)
$xmlvalue=$stuff.SelectSingleNode($xpath,$ns)
错误-需要命名空间管理器或XsltContext

如果

$xmlvalue=$stuff.SelectSingleNode($xpath,'SOAP'))
错误-找不到“SelectSingleNode”的重载


如何使xpath查询与名称空间一起工作?

我看不到定义了
$xmlns
。这项工作:

[string]$xpath='/SOAP:Envelope/SOAP:Body/P2MessageServiceStatus/CONNECTION'

$namespaceMgr = New-Object System.Xml.XmlNamespaceManager $stuff.NameTable
$namespace = $stuff.DocumentElement.NamespaceURI
$namespaceMgr.AddNamespace("SOAP", $namespace)


$stuff.SelectSingleNode($xpath,$ns)
注意:您还可以使用
PowerShell
访问节点等属性:

$stuff.DocumentElement.Body.P2MessageServiceStatus.CONNECTION

如果我使用$stuff.SelectSingleNode($xpath,$namespace),它根本不起作用。如果$stuff.SelectSingleNode($xpath,$namespaceMgr),它将返回值,但我也遇到异常“找不到SelectSingleNode和2个方法的重新加载…”。。。这是正常的吗?天哪,我在工作电脑上得到了这个异常,但在我的服务器上没有。。。非常感谢,如果我看到变量$namespaceMgr,我将看到其中的3个变量——xmlns、xml和SOAP。这可能是一个错误的原因吗?