我只是尝试解析SOAP响应并从以下XML中提取 ResponseCode 和 UnconfirmedReasonCode 元素:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CoverageResponse xmlns="http://www.iicmva.com/CoverageVerification/">
<Detail>
<PolicyInformation>
<CoverageStatus>
<ResponseDetails>
<ResponseCode>CONFIRMED</ResponseCode>
<UnconfirmedReasonCode/>
</ResponseDetails>
</CoverageStatus>
</PolicyInformation>
</Detail>
</CoverageResponse>
</soap:Body>
</soap:Envelope>
我一直在尝试做的事情不起作用:
Dim doc As New XmlDocument
doc.LoadXml(result)
Dim root = doc.DocumentElement.FirstChild.FirstChild
Dim responseDetails = root.SelectSingleNode("descendant::Detail/PolicyInformation/CoverageStatus/ResponseDetails")
Dim responseCode = responseDetails.ChildNodes(0).InnerText
Dim unconfirmedReasonCode = responseDetails.ChildNodes(1).InnerText
Console.WriteLine("Response Details:" & vbCrLf & vbCrLf & responseCode & " " & unconfirmedReasonCode)
Console.ReadLine()
答案 0 :(得分:2)
这是关于选择具有默认命名空间的XML文档元素的最常见问题 - 请搜索XPath和默认命名空间。 提示:了解XmlNamespaceManager类。
一种相对简单且不易阅读的选择方法:
使用:
/*/*/*/*/*/*/*/*[local-name()='ResponseCode']
并使用:
/*/*/*/*/*/*/*/*[local-name()='UnconfirmedReasonCode']
基于XSLT的验证:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"/*/*/*/*/*/*/*/*[local-name()='ResponseCode']"/>
<xsl:copy-of select=
"/*/*/*/*/*/*/*/*[local-name()='UnconfirmedReasonCode']"/>
</xsl:template>
</xsl:stylesheet>
将此转换应用于提供的XML文档:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CoverageResponse xmlns="http://www.iicmva.com/CoverageVerification/">
<Detail>
<PolicyInformation>
<CoverageStatus>
<ResponseDetails>
<ResponseCode>CONFIRMED</ResponseCode>
<UnconfirmedReasonCode/>
</ResponseDetails>
</CoverageStatus>
</PolicyInformation>
</Detail>
</CoverageResponse>
</soap:Body>
</soap:Envelope>
输出两个正确选择的节点:
<ResponseCode xmlns="http://www.iicmva.com/CoverageVerification/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">CONFIRMED</ResponseCode>
<UnconfirmedReasonCode xmlns="http://www.iicmva.com/CoverageVerification/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />