从Invoke-WebRequest的内容响应进行XML解析

时间:2019-07-18 07:54:55

标签: xml powershell

我正在尝试获取XML结果,但不像我那样在字符串中:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://#####">
    <SOAP-ENV:Body>
        <ns0:ResumeOrderResponse xmlns:ns0="http://####" xmlns:ns1="http://######" ns1:transactionID="*********-">
            <ns0:ResponseStatus ns1:code="1">
                <ns1:message>Order  not found</ns1:message>
            </ns0:ResponseStatus>
        </ns0:ResumeOrderResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这是我的处理方式:

$header = @{"Authorization" = '********';"SOAPAction"='ResumeOrder'}
$post = Invoke-WebRequest -Uri $uri -Headers $header -Method Post -Body $xml -ContentType "application/soap+xml"
$bn = [xml]$post.Content

Write-Output $bn

我正在尝试仅获取元素“ ns1:message”中的行,在本例中为“找不到订单”。

1 个答案:

答案 0 :(得分:2)

您可以提取“ ns1:message”值,

$bn.Envelope.Body.ResumeOrderResponse.ResponseStatus.message

希望有帮助!

这是我在本地测试的样本,

$xmlString = [xml]@'
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://#####">
    <SOAP-ENV:Body>
        <ns0:ResumeOrderResponse xmlns:ns0="http://####" xmlns:ns1="http://######" ns1:transactionID="*********-">
            <ns0:ResponseStatus ns1:code="1">
                <ns1:message>Order  not found</ns1:message>
            </ns0:ResponseStatus>
        </ns0:ResumeOrderResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
'@


$xmlString.Envelope.Body.ResumeOrderResponse.ResponseStatus.message