首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回空值的XML DeSerialization

返回空值的XML DeSerialization
EN

Stack Overflow用户
提问于 2017-04-06 16:14:52
回答 2查看 262关注 0票数 0

我在一个web api中有一个POST方法,它有一个RequestAppointment参数。例如:

代码语言:javascript
复制
 // POST: api/RequestAppointment
            public HttpResponseMessage Post([FromBody]RequestAppointment httpRequest)
            {
                var response = Appointment.CreateAppointment(httpRequest);

                return Request.CreateResponse<ResponseRequest>(System.Net.HttpStatusCode.Created, response); 
            } 

RequestAppoint类的结构如下:

代码语言:javascript
复制
public partial class RequestAppointment
    {
        public RequestAppointmentAppointmentInformation appointmentInformation { get; set; }      
    }
    public partial class RequestAppointmentAppointmentInformation
    {
        public string AppointmentCcEmail { get; set; }        
        public string ClaimType { get; set; }       
        public RequestAppointmentAppointmentInformationClaimant Claimant { get; set; }

    }
    public partial class RequestAppointmentAppointmentInformationClaimant
    {
        public System.DateTime DateOfBirth { get; set; }        
    }

可以提出两个请求:第一个请求是:

代码语言:javascript
复制
<RequestAppointment>
  <appointmentInformation>
    <AppointmentCcEmail>x.x.com</AppointmentCcEmail>   
    <ClaimType>A</ClaimType>
    <Claimant>
      <DateOfBirth>1961-12-25</DateOfBirth>
    </Claimant>
  </appointmentInformation>
</RequestAppointment>

第二个请求是:

代码语言:javascript
复制
<RequestAppointment>
  <appointmentInformation>
    <AppointmentCcEmail>x.x.com</AppointmentCcEmail>   
    <ClaimType>A</ClaimType>   
  </appointmentInformation>
</RequestAppointment>

两个请求XML之间的不同之处在于#1有声明人标签,而#2没有。

第二个请求没有,因为当用户从数据接口调用web服务时,没有提供索赔人。而当提供索赔人时,第一个将始终存在。

在我使用Fiddler进行测试时,我的#1请求返回数据。也就是说,它能够反序列化为对象RequestAppointment,而#2请求返回空值。

我能做些什么来解决这个问题?请求将始终以这两种格式出现,并且它们将始终使用相同的模型结构。

我能做些什么来解决这个问题?我在Fiddler中测试了这个场景。

EN

回答 2

Stack Overflow用户

发布于 2017-04-06 16:29:11

首先,步骤1。如果你有XSD文件,最好的解决方案是使用xsd.exe see haw to use it生成类,如果没有,你可以自己创建XSD并执行步骤1,或者最后你应该做出改变,并指出显式属性使你的类RequestAppointment可序列化。有很多关于它的文章。

这个类是用xsd.exe生成的,我从您的XML中创建了示例模式,只在标记<Claimant>中进行了更改,使其成为可选的,类看起来像这样。注意工具如何生成可选字段Claimant

代码语言:javascript
复制
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.6.1055.0.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class RequestAppointment {

    private RequestAppointmentAppointmentInformation appointmentInformationField;

    /// <remarks/>
    public RequestAppointmentAppointmentInformation appointmentInformation {
        get {
            return this.appointmentInformationField;
        }
        set {
            this.appointmentInformationField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestAppointmentAppointmentInformation {

    private string appointmentCcEmailField;

    private string claimTypeField;

    private RequestAppointmentAppointmentInformationClaimant claimantField;

    /// <remarks/>
    public string AppointmentCcEmail {
        get {
            return this.appointmentCcEmailField;
        }
        set {
            this.appointmentCcEmailField = value;
        }
    }

    /// <remarks/>
    public string ClaimType {
        get {
            return this.claimTypeField;
        }
        set {
            this.claimTypeField = value;
        }
    }

    /// <remarks/>
    public RequestAppointmentAppointmentInformationClaimant Claimant {
        get {
            return this.claimantField;
        }
        set {
            this.claimantField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestAppointmentAppointmentInformationClaimant {

    private System.DateTime dateOfBirthField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType="date")]
    public System.DateTime DateOfBirth {
        get {
            return this.dateOfBirthField;
        }
        set {
            this.dateOfBirthField = value;
        }
    }
}

XSD示例

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="RequestAppointment">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="appointmentInformation">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="AppointmentCcEmail" type="xs:string" />
              <xs:element name="ClaimType" type="xs:string" />
              <xs:element name="Claimant" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="DateOfBirth" type="xs:date" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
票数 0
EN

Stack Overflow用户

发布于 2017-04-07 14:51:56

谢谢你的所有建议。他们太棒了!Seyran的答案真的很棒。但在我的问题中,我通过对所有元素“真”(nil=) (nullable=true)解决了它。我使用xsd为我的请求xml生成类。

谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43249487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档