Restsharp反序列化其他属性名称

时间:2019-04-27 10:11:05

标签: xml serialization deserialization restsharp

我有一个定义为此的类:

[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; }

}

并且已正确反序列化。

但是现在,我需要将属性IPGApiOrderResponse更改为其他内容,例如Response,而无需更改XML属性,如下所示:

[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; }

}

但是,更改属性名称后,反序列化时它始终为null。我想念什么?

编辑1: 以下是我要反序列化的XML:

<?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 个答案:

答案 0 :(得分:0)

以下作品:

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
    {
    }

}