将序列化数据传递给web api时数据丢失

时间:2016-04-04 14:59:03

标签: xml serialization asp.net-web-api

我将以下数据传递给Web API。当它进入我的API调用时,我有数据的“客户端”部分,我有“状态”容器,但它没有信息。

 <Client>
<ContactNumber>1</ContactNumber>
<Name>Test Name</Name>
<ProcessLevel>Complete</ProcessLevel>
<ResponseLevel>Minimal</ResponseLevel>
</Client>
<Status>
<MyId>010111111</MyId>
<MyId>010122211</MyId>
 </Status>

我期望在网络API上发布的课程:

 public partial class StatusRequest
{
    public StatusRequest()
    {
        this.Client = new Client();
        this.Status = new List<string>();
    }

    public Client Client { get; set; }       
    [XmlArrayItem("MyId")]
    public List<string> Status { get; set; }
}

结果以

显示
<Client>
<ContactNumber>1</ContactNumber>
<Name>Test Name</Name>
<ProcessLevel>Complete</ProcessLevel>
<ResponseLevel>Minimal</ResponseLevel>
</Client>
<Status></Status>

我错过了什么?为什么“状态”部分为空?

2 个答案:

答案 0 :(得分:1)

您用来装饰模型的XmlArrayItem属性只有XmlSerializer类可以理解。默认情况下,IIRC ASP.NET Web API不使用此序列化程序,而是DataContractSerializer(它反过来不支持对XML格式的这种细粒度控制)。

因此,如果您希望将您的设置考虑在内,请确保在引导它时指示Web API使用正确的序列化程序:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...
        config.Formatters.XmlFormatter.UseXmlSerializer = true;
    }
}

答案 1 :(得分:0)

You come up with your own custom serializer. First, you need to create a model as like below.

    public class StatusRequest
    {
      public StatusRequest()
        {
            //this.Client = new Client();
            this.Status = new List<string>();
        }

        //public Client Client { get; set; }

        [XmlArray("Status"), XmlArrayItem("MyId")]
        public List<String> Status { get; set; }
    }

Then create a class that is inherited from XmlObjectSerializer as below.

public class StatusRequestSerializer : XmlObjectSerializer
{
    XmlSerializer serializer;

    public StatusRequestSerializer()
    {
        this.serializer = new XmlSerializer(typeof(StatusRequest));
    }

    public override void WriteObject(XmlDictionaryWriter writer, object graph)
    {
        var xmlSerializerNamespaces = new XmlSerializerNamespaces();
        xmlSerializerNamespaces.Add("", "");
        serializer.Serialize(writer, graph, xmlSerializerNamespaces);
    }

    public override bool IsStartObject(XmlDictionaryReader reader)
    {
        throw new NotImplementedException();
    }

    public override object ReadObject(XmlDictionaryReader reader, bool verifyObjectName)
    {
        throw new NotImplementedException();
    }

    public override void WriteEndObject(XmlDictionaryWriter writer)
    {
        throw new NotImplementedException();
    }

    public override void WriteObjectContent(XmlDictionaryWriter writer, object graph)
    {
        throw new NotImplementedException();
    }

    public override void WriteStartObject(XmlDictionaryWriter writer, object graph)
    {
        throw new NotImplementedException();
    }
}

You need to implement others method as you need. Now you need to add below lines of code in WebApiConfig.

    config.Formatters.XmlFormatter.SetSerializer<StatusRequest>(new StatusRequestSerializer());
    config.Formatters.XmlFormatter.UseXmlSerializer = true;