2015-10-13 105 views
1

如何解析此XML文件?我需要得到確認和標記值解析C#中的PayPal SOAP響應

我已經試過

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

我的XML字符串是好的,下面,但確認和令牌爲空。

<?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> 

回答

2

如果您正在與肥皂服務交互,您可能需要添加服務引用並讓它爲您創建代理類。

使用LINQ到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 
        } 
        ); 

或更好,但

0

 XmlDocument doc = new XmlDocument(); 
 
     XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable); 
 
     mgr.AddNamespace("PayPalAPI", "urn:ebay:api:PayPalAPI"); 
 
     mgr.AddNamespace("eBLBaseComponents", "urn:ebay:apis:eBLBaseComponents"); 
 
     doc.LoadXml(xml); 
 
     XmlNode Ack = doc.SelectSingleNode("//eBLBaseComponents:Ack", mgr); 
 
     XmlNode node = doc.SelectSingleNode("//PayPalAPI:SetExpressCheckoutResponse", mgr); 
 
     
 
     string strAck = null; 
 
     if (Ack != null) 
 
      strAck = Ack.InnerText.ToLower(); 
 
     if (strAck != null && (strAck == "success" || strAck == "successwithwarning")) 
 
     { 
 
      //token = decoder["TOKEN"]; 
 
      foreach (XmlNode child in node.ChildNodes) 
 
      { 
 
       if (child.Name.ToLower() == "token") 
 
       { 
 
        token = child.InnerText; 
 
       } 
 
      } 
 
     }