0

I'm passing the following data in to a Web API. When it gets to my API call I have the "Client" section of the data and I have the "Status" container but it has no information in it.

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

The class that I am expecting on the web 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; }
}

The results come in as

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

What am I missing? Why is the Status section empty?

1
  • Instead of "List<string>", try List<MyOtherMiniObject> and put a string property on MyOtherMiniObject. Commented Apr 4, 2016 at 15:02

2 Answers 2

1

The XmlArrayItem attribute that you used to decorate your model with is something that only the XmlSerializer class understands. IIRC by default ASP.NET Web API doesn't use this serializer but rather the DataContractSerializer (which in turns doesn't support such fine grained control over the XML format).

So if you want your settings to be taken into account make sure that you have instructed the Web API to use the proper serializer when bootstrapping it:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        ...
        config.Formatters.XmlFormatter.UseXmlSerializer = true;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.