1
Vote

Exception in Method Decode(string soapEnvelope, Type toType) line (object)serializer.Deserialize(reader);

description

error in the class SoapEncoder in method Decode in line 96 return (object)serializer.Deserialize(reader);
 
public static object Decode(string soapEnvelope, Type toType)
    {
        XmlSerializer serializer = null;
 
        try
        {
            /// Initializing the XMLSerializer.
            serializer = new XmlSerializer(toType);
 
            /// Removing SOAP outer Envelope
            soapEnvelope = soapEnvelope.Replace("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header /><soapenv:Body>", string.Empty);
            soapEnvelope = soapEnvelope.Replace("</soapenv:Body></soapenv:Envelope>", string.Empty);
            soapEnvelope = soapEnvelope.Replace("xmlns:ns2=\"http://svcs.paypal.com/types/ap\"", string.Empty);
            soapEnvelope = soapEnvelope.Replace("ns2:", string.Empty);
            soapEnvelope = soapEnvelope.Replace("soapenv:", string.Empty);
            soapEnvelope = soapEnvelope.Replace("ns3:", string.Empty);
            soapEnvelope = soapEnvelope.Replace("xmlns:ns2=\"http://svcs.paypal.com/types/ap\"", string.Empty);
 
            /// Deserializing and Returning the XML
            using (MemoryStream reader = new MemoryStream(Encoding.UTF8.GetBytes(soapEnvelope)))
            {
                return (object)serializer.Deserialize(reader); //Error here
            }                
        }
        catch (FATALException)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw new FATALException("Error occurred in SoapEncoder->Decode method", ex);
        }
        finally
        {
            serializer = null;
        }
    }
 
soapEnvelope xml:
 
<xml version='1.0' encoding='utf-8'?>
<Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">
    <Header/>
    <Body>
        <PayResponse >
            <responseEnvelope>
                <timestamp>2011-08-09T00:28:53.399-07:00</timestamp>
                <ack>Success</ack>
                <correlationId>621854fd57929</correlationId>
                <build>2012864</build>
            </responseEnvelope>
            <payKey>AP-1SE162159L922805T</payKey>
            <paymentExecStatus>COMPLETED</paymentExecStatus>
    </PayResponse>

comments

Orgbrat wrote Oct 23, 2011 at 8:40 PM

You are only trying to deserialize the PayResponse object so fix the first "soapEnvelope.Replace" so that the XML loaded into the Memory stream is as below...

soapEnvelope xml after SOAP outer Envelope removal:

<xml version='1.0' encoding='utf-8'?>
<PayResponse >
<responseEnvelope>
<timestamp>2011-08-09T00:28:53.399-07:00</timestamp>
<ack>Success</ack>
<correlationId>621854fd57929</correlationId>
<build>2012864</build>
</responseEnvelope>
<payKey>AP-1SE162159L922805T</payKey>
<paymentExecStatus>COMPLETED</paymentExecStatus>
</PayResponse>

wrote Feb 14, 2013 at 7:14 PM