SOAP信封xmlelement directcast无法正常工作 [英] SOAP envelope xmlelement directcast not working
问题描述
我成功使用了Web服务SOAP信封XML响应。我在底部加了一个缩短的版本。
(#1)当我使用时:
I'm successfully consuming a web-service SOAP envelope XML response. I've included a shortened version at the bottom.
(#1) when I use:
Dim totalcost1 As String = cfnroot.SelectNodes("GetRateQuoteResponse/GetRateQuoteResult/TotalCharges").Item(0).InnerText
我得到的值是2597.01这是正确的。
(#2)当我使用:
I'm getting a value of 2597.01 which is correct.
(#2) When I use:
im totalcost As XmlElement = DirectCast(CFLxmldoc.SelectSingleNode("df:GetRateQuoteResponse/df:GetRateQuoteResult/df:TotalCharges", mgr), XmlElement)
我根本没有得到任何值。
我更喜欢使用#2的语法并且总是使用它。我无法弄清楚为什么我没有从#2获得任何价值。我发现测试空值或不存在的节点更容易。这是我第一次使用SOAP XML信封。通常使用REST / URL。
I'm not getting a value at all.
I prefer to use #2's syntax and always have used it. I can't figure out why i'm not getting any value from #2. I find it easier to test for empty values or nodes that aren't present. This is my first time using a SOAP XML envelope. Normally use REST/URL.
<?xml version="1.0"?>
<SOAP-ENV:Body xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<GetRateQuoteResponse xmlns="">
<GetRateQuoteResult>
<Date>03/22/2019</Date>
<Time>12:06:22</Time>
<DeficitAmount>152.84</DeficitAmount>
<TotalCharges>2597.01</TotalCharges>
<NetCharges>918.91</NetCharges>
</GetRateQuoteResult>
</GetRateQuoteResponse>
</SOAP-ENV:Body>
我的尝试:
What I have tried:
Dim CFLresponse = CFLService.GetRateQuoteXml(CFLRaterequest)
Dim ResponseXML As String = CFLresponse.InnerXml
Dim CFLxmldoc As XmlDocument = New XmlDocument
CFLxmldoc.LoadXml(ResponseXML)
Dim mgr As New XmlNamespaceManager(CFLxmldoc.NameTable)
mgr.AddNamespace("df", CFLxmldoc.DocumentElement.NamespaceURI)
Dim cfnroot As XmlElement = CFLxmldoc.DocumentElement
Dim totalcost As XmlElement = DirectCast(CFLxmldoc.SelectSingleNode("df:GetRateQuoteResponse/df:GetRateQuoteResult/df:TotalCharges", mgr), XmlElement)
Dim totalcost1 As String = cfnroot.SelectNodes("GetRateQuoteResponse/GetRateQuoteResult/TotalCharges").Item(0).InnerText
推荐答案
两个问题:
1)您的SelectSingleNode
调用从文档开始,而你的Sel ectNodes
从根节点开始呼叫。
2)您的SelectSingleNode
呼叫是在http://schemas.xmlsoap.org/soap/envelope/
命名空间中查找节点,而您的SelectNodes
call正在寻找没有命名空间的节点。
尝试:
Two problems:
1) YourSelectSingleNode
call starts from the document, whereas yourSelectNodes
call starts from the root node.
2) YourSelectSingleNode
call is looking for nodes in the"http://schemas.xmlsoap.org/soap/envelope/"
namespace, whereas yourSelectNodes
call is looking for nodes with no namespace.
Try:
Dim totalcost As XmlElement = DirectCast(cfnroot.SelectSingleNode("GetRateQuoteResponse/GetRateQuoteResult/TotalCharges"), XmlElement)
这篇关于SOAP信封xmlelement directcast无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!