I have a Class that is defined as this:

[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class OrderResponseBody
{
    [XmlElement(ElementName = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Fault Fault { get; set; }

    [XmlElement(ElementName = "IPGApiOrderResponse", Namespace = "http://ipg-online.com/ipgapi/schemas/ipgapi")]
    public IPGApiOrderResponse IPGApiOrderResponse { get; set; }

}

and it is deserialized correctly.

But now, I need to change the Property IPGApiOrderResponse to something else, like Responsewithout changing the XML attribute, something like this:

[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class OrderResponseBody
{
    [XmlElement(ElementName = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public Fault Fault { get; set; }

    [XmlElement(ElementName = "IPGApiOrderResponse", Namespace = "http://ipg-online.com/ipgapi/schemas/ipgapi")]
    public IPGApiOrderResponse Response { get; set; }

}

But, after changing the Property name, it is always null when deserialized. What Am I missing?

Edit 1: Here are the XML that I’m trying to deserialize:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header />
   <SOAP-ENV:Body>
  <ipgapi:IPGApiOrderResponse xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi" xmlns:a1="http://ipg-online.com/ipgapi/schemas/a1" xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1">
     <ipgapi:ApprovalCode>Y:827973:4523631795:PPX :497049</ipgapi:ApprovalCode>
     <ipgapi:AVSResponse>PPX</ipgapi:AVSResponse>
     <ipgapi:Brand>MASTERCARD</ipgapi:Brand>
     <ipgapi:Country>BRA</ipgapi:Country>
     <ipgapi:CommercialServiceProvider>BIN</ipgapi:CommercialServiceProvider>
     <ipgapi:OrderId>A1556224292156889196</ipgapi:OrderId>
     <ipgapi:IpgTransactionId>84523631795</ipgapi:IpgTransactionId>
     <ipgapi:PaymentType>CREDITCARD</ipgapi:PaymentType>
     <ipgapi:ProcessorApprovalCode>827973</ipgapi:ProcessorApprovalCode>
     <ipgapi:ProcessorReferenceNumber>000067462</ipgapi:ProcessorReferenceNumber>
     <ipgapi:ProcessorResponseCode>00</ipgapi:ProcessorResponseCode>
     <ipgapi:ProcessorResponseMessage>APROVADA 000067462</ipgapi:ProcessorResponseMessage>
     <ipgapi:TDate>1556224292</ipgapi:TDate>
     <ipgapi:TDateFormatted>2019.04.25 22:31:32 (CEST)</ipgapi:TDateFormatted>
     <ipgapi:TerminalID>EII00714</ipgapi:TerminalID>
     <ipgapi:TransactionResult>APPROVED</ipgapi:TransactionResult>
     <ipgapi:TransactionTime>1556224292</ipgapi:TransactionTime>
  </ipgapi:IPGApiOrderResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

1 Answers

0
jdweng On

The following works :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));

            Envelope envelope = (Envelope)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement("Body")]
        public OrderResponseBody OrderResponseBody { get; set; }
    }
    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class OrderResponseBody
    {
        [XmlElement(ElementName = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Fault Fault { get; set; }

        [XmlElement(ElementName = "IPGApiOrderResponse", Namespace = "http://ipg-online.com/ipgapi/schemas/ipgapi")]
        public IPGApiOrderResponse Response { get; set; }

    }
    public class IPGApiOrderResponse
    {
        public string ApprovalCode { get; set; } 
    }
    public class Fault
    {
    }

}