gpt4 book ai didi

c# - 在 C# 中解析 PayPal SOAP 响应

转载 作者:太空宇宙 更新时间:2023-11-03 15:55:01 25 4
gpt4 key购买 nike

如何解析这个 XML 文件?我需要获取 Ack 和 Token 值

我试过了

XmlDocument doc = new XmlDocument()
doc.LoadXml(xml)
XmlNode Ack = doc.DocumentElement.SelectSingleNode("//Ack")
XmlNode Token = doc.DocumentElement.SelectSingleNode("//Token")

我的 xml 字符串如下所示,但 Ack 和 Token 为空。

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ed="urn:ebay:apis:EnhancedDataTypes" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:ns="urn:ebay:api:PayPalAPI">
<SOAP-ENV:Header>
<Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" xsi:type="wsse:SecurityType"></Security>
<RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType">
<Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType">
<Username xsi:type="xs:string"></Username>
<Password xsi:type="xs:string"></Password>
<Signature xsi:type="xs:string"></Signature>
<Subject xsi:type="xs:string"></Subject>
</Credentials>
</RequesterCredentials>
</SOAP-ENV:Header>
<SOAP-ENV:Body id="_0">
<SetExpressCheckoutResponse xmlns="urn:ebay:api:PayPalAPI">
<Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2015-10-09T21:21:39Z</Timestamp>
<Ack xmlns="urn:ebay:apis:eBLBaseComponents">Success</Ack>
<CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">fsfsd8d6bb</CorrelationID>
<Version xmlns="urn:ebay:apis:eBLBaseComponents">112</Version>
<Build xmlns="urn:ebay:apis:eBLBaseComponents">18sdf778</Build>
<Token xsi:type="ebl:ExpressCheckoutTokenType">EC-6YBsdfs24894B</Token>
</SetExpressCheckoutResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

最佳答案

如果您正在与 soap 服务交互,您可能希望添加服务引用并让它为您创建代理类。

使用 Linq to XML,您可以执行如下操作

    var parsedSoap = XElement.Parse(soapMessage);
XNamespace payPalResponseNs ="urn:ebay:api:PayPalAPI";
XNamespace resultingResponseNs = "urn:ebay:apis:eBLBaseComponents";
parsedSoap.Descendants()
.Where(x=> x.Name == payPalResponseNs+"SetExpressCheckoutResponse")
.Select(x=> new
{
Timestamp = x.Element(resultingResponseNs +"Timestamp")?.Value,
Ack = x.Element(resultingResponseNs +"Ack")?.Value,
CorrelationID = x.Element(resultingResponseNs +"CorrelationID")?.Value,
Version = x.Element(resultingResponseNs +"Version")?.Value,
Build = x.Element(resultingResponseNs +"Build")?.Value,
Token = x.Element(payPalResponseNs +"Token")?.Value
}
);

或者更好

关于c# - 在 C# 中解析 PayPal SOAP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33109800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com