使用命名空间

时间:2016-05-26 04:16:14

标签: xml powershell

有一些代码

123.xml:

<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP:Body>
    <P2MessageServiceStatus>
      <CONNECTION>CONNECTION_CONNECTED</CONNECTION>
      <ROUTER>ROUTER_CONNECTED</ROUTER>
    </P2MessageServiceStatus>
  </SOAP:Body>
</SOAP:Envelope>

所以,我正在尝试使用名称Spaces获取价值。没有它它运行正常,但使用命名空间它什么都不做

$url='http://123.xml'
$xpath='/SOAP:Envelope/SOAP:Body/P2MessageServiceStatus/@CONNECTION'
$namespace='SOAP'

$wc = New-Object  Net.WebClient
[xml]$xml_body = $wc.DownloadString($url)
$xmlvalue= Select-Xml -XPath $xpath -Xml $xml_body -Namespace @{ SOAP = 'http://test'}

$ xmlvalue将为空且没有错误,如何解决?

1 个答案:

答案 0 :(得分:2)

这里有两件事:

  1. 命名空间不对
  2. 连接不是属性,因此不需要@
  3. 这对我有用:

    $xml = [xml] (gc e:\1.txt )
    $xpath='/SOAP:Envelope/SOAP:Body/P2MessageServiceStatus/CONNECTION'
    $xmlvalue= Select-Xml -XPath $xpath -Xml $xml -Namespace @{ "SOAP" = 'http://schemas.xmlsoap.org/soap/envelope/'}
    $xmlvalue