Pergunta

I am having trouble deserializing a Rest XML response into its object. If I serialize the data contract that I am expecting in the consumer application, the XML formatting is slightly different than what is actually being received from the Rest service. The data contract class that is sent from the service is the exact same as the one that I am expecting. If I change the XML received from the Rest service to the response expected by the consumer application, everything works.

The response that I am receiving from the Rest service is below.

<GeneralInquiryResponseCollection xmlns=\"http://schemas.datacontract.org/2004/07/MEA.SmartApp.AdminGateway.ServiceContract.Responses\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
  <ResultCode i:nil=\"true\"/>
  <ResultCodeDescription i:nil=\"true\"/>
  <ResultInfoDescription i:nil=\"true\"/>
  <ErrorMessage/>
  <IsSuccess>true</IsSuccess>
  <TransactionStatus>
    <Description>Transaction Successful</Description>
    <StatusCode>2000</StatusCode>
  </TransactionStatus>
  <AsOfDate>9999-12-31T00:00:00</AsOfDate>
  <FunctionalGroupingCode>302</FunctionalGroupingCode>
  <GeneralInquiryResponseContainer>
  </GeneralInquiryResponseContainer>
  <LastWMAValuationDate>0001-01-01T00:00:00</LastWMAValuationDate>
  <PolicyNumber>551372461</PolicyNumber>
  <TransactionExecutionDateTime>2014-01-14T15:05:05.859558</TransactionExecutionDateTime>
</GeneralInquiryResponseCollection>

The XML that I get when I serialize from the expected data contract is below.

<?xml version=\"1.0\"?>
<GeneralInquiryResponseCollection xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
  <ResultCode />
  <ResultCodeDescription>Success</ResultCodeDescription>
  <ResultInfoDescription>Result Info Description</ResultInfoDescription>
  <IsSuccess>true</IsSuccess>
  <TransactionStatus>
    <Description>Transaction Successful</Description>
    <StatusCode>2000</StatusCode>
    <HTTPStatusCode>OK</HTTPStatusCode>
  </TransactionStatus>
  <StatusCode>2000</StatusCode>
  <AsOfDate>9999-12-31T23:59:59.9999999</AsOfDate>
  <FunctionalGroupingCode>302</FunctionalGroupingCode>
  <LastWMAValuationDate>0001-01-01T00:00:00</LastWMAValuationDate>
  <PolicyNumber>551372461</PolicyNumber>
  <TransactionExecutionDateTime>2014-01-14T14:40:59.0930535-07:00</TransactionExecutionDateTime>
</GeneralInquiryResponseCollection>

The main differences between them are as follows.

  1. Consumer expects - <?xml version=\"1.0\"?> - but Rest service does not send this.

  2. Consumer expects - <GeneralInquiryResponseCollection xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">

Rest service sends <GeneralInquiryResponseCollection xmlns=\"http://schemas.datacontract.org/2004/07/MEA.SmartApp.AdminGateway.ServiceContract.Responses\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">

  1. Consumer expects that properties of null value does not have the tag in the XML, but the Rest service sends the tag with i:nil=\"true\".

The code that deserializes is below where T is a GeneralInquiryResponseCollection.

using (var ResponseStream = Response.GetResponseStream())
{
    XmlSerializer Serializer = new XmlSerializer(typeof(T));
    return (T)Serializer.Deserialize(ResponseStream);
}

Below is the service contract

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using MEA.SmartApp.AdminGateway.Interfaces;

namespace MEA.SmartApp.AdminGateway.ServiceContract.Responses
{
    [DataContract]
    public class GeneralInquiryResponseCollection : WMAResponse, IWMAResponse
    {

        #region Data Members

        [DataMember]
        public DateTime AsOfDate { get; set; }

        [DataMember]
        public string FunctionalGroupingCode { get; set; }

        [DataMember]
        public List<GeneralInquiryResponse> GeneralInquiryResponseContainer { get; set; }

        [DataMember]
        public DateTime LastWMAValuationDate { get; set; }

        [DataMember]
        public string PolicyNumber { get; set; }

        [DataMember]
        public DateTime? TransactionExecutionDateTime { get; set; }

        #endregion
    }
}

GeneralInquiryResponse

using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using MEA.SmartApp.AdminGateway.Interfaces;

namespace MEA.SmartApp.AdminGateway.ServiceContract.Responses
{
    [DataContract(Namespace = "")]
    [XmlInclude(typeof(ClientResponse))]
    [XmlInclude(typeof(ClientRoleResponse))]
    [XmlInclude(typeof(DCAFundResponse))]
    [XmlInclude(typeof(DCAResponse))]
    [XmlInclude(typeof(GMABResponse))]
    [XmlInclude(typeof(GMDBResponse))]
    [XmlInclude(typeof(GMIBResponse))]
    [XmlInclude(typeof(GWBResponse))]
    [XmlInclude(typeof(InterestRateResponse))]
    [XmlInclude(typeof(MoneyFundSegmentResponse))]
    [XmlInclude(typeof(MRDFundResponse))]
    [XmlInclude(typeof(PayoutFundResponse))]
    [XmlInclude(typeof(PayoutResponse))]
    [XmlInclude(typeof(PolicyFundResponse))]
    [XmlInclude(typeof(PolicyResponse))]
    [XmlInclude(typeof(PremiumBonusTransactionHistoryResponse))]
    [XmlInclude(typeof(ProducerGroupSegmentResponse))]
    [XmlInclude(typeof(RebalancingResponse))]
    [XmlInclude(typeof(RebalancingScheduledFundResponse))]
    [XmlInclude(typeof(RebalancingUnscheduledFundResponse))]
    [XmlInclude(typeof(WithdrawalGrossTransactionHistoryResponse))]
    [XmlInclude(typeof(WithdrawalSurrenderTransactionHistoryResponse))]
    public abstract class GeneralInquiryResponse : IGeneralInquiryResponse
    {
        #region Data Members

        [DataMember]
        public string ResponseIdentifier { get; set; }

        #endregion
    }
}

Ive been struggling with this one for a while and Im hitting dead ends. Any suggestions?

Foi útil?

Solução

This means ResultCode is null

<ResultCode i:nil=\"true\"/>

This means ResultCode = ""

<ResultCode />

If you aren't just inquiring about nil, then please explain in more detail. It's just a nillable property, defined as such in the schema.

To remove your namespace:

[DataContract(Namespace = "")]

Outras dicas

You can use the "ShouldSerializeXXX" method in your data model. See this MSDN article

public bool ShouldSerializeResultCode() 
{
   return ResultCode != null;
}

For data contracts you could use the [DataMember(EmitDefaultValue = false)] attribute instead of adding a method. See Data members default values.

[DataMember(EmitDefaultValue = false)]
public string ResultCode { get; set; }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top