从Rest服务反序列化XML

时间:2014-01-14 22:57:37

标签: c# xml rest deserialization xml-deserialization

我无法将Rest XML响应反序列化到其对象中。如果我序列化我在消费者应用程序中期望的数据协定,则XML格式与实际从Rest服务接收的格式略有不同。从服务发送的数据协定类与我期望的完全相同。如果我将从Rest服务收到的XML更改为消费者应用程序所期望的响应,那么一切正常。

我从Rest服务收到的回复如下。

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

我从预期数据合同序列化时获得的XML如下所示。

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

它们之间的主要区别如下。

  1. 消费者期望 - <?xml version=\"1.0\"?> - 但Rest服务不会发送此信息。

  2. 消费者期望 - <GeneralInquiryResponseCollection xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">

  3. 休息服务发送<GeneralInquiryResponseCollection xmlns=\"http://schemas.datacontract.org/2004/07/MEA.SmartApp.AdminGateway.ServiceContract.Responses\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">

    1. Consumer希望null值的属性在XML中没有标记,但Rest服务使用i:nil = \“true \”发送标记。
    2. 反序列化的代码低于T是GeneralInquiryResponseCollection的地方。

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

      以下是服务合同

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

      我一直在努力争取这一段时间,我正在走向死胡同。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

这意味着ResultCode为空

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

这意味着ResultCode =“”

<ResultCode />

如果您不只是询问nil,请详细解释。它只是一个nillable属性,在模式中定义为。

删除命名空间:

[DataContract(Namespace = "")]

答案 1 :(得分:0)

您可以在数据模型中使用“ShouldSerializeXXX”方法。见MSDN article

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

对于数据合同,您可以使用[DataMember(EmitDefaultValue = false)]属性而不是添加方法。请参阅Data members default values

[DataMember(EmitDefaultValue = false)]
public string ResultCode { get; set; }