0

Am currently trying to use the Web API and accept a HTTP POST containing an XML. I was under the impression that this could be automatically deserialized into an object, but I am missing something.

public class Item
{
    public int Value { get; set; }
}

This one works, I receive the XML from SoapUI and can act upon it

[HttpPost]
public HttpResponseMessage Post(int id, HttpRequestMessage request)
{
    var doc = new XmlDocument();
    doc.Load(request.Content.ReadAsStreamAsync().Result);

    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new Item() { Value = id });
    return response;
}

I'd like to have this though:

[HttpPost]
public HttpResponseMessage Post(int id, [FromBody]List<Item> items)
{
    return Request.CreateResponse(HttpStatusCode.OK);
}

Trying to post this:

<Items><Item><Value>1</Value></Item></Items>

What did I miss?

1
Contributor
1
Reply
20
Views
2 Years
Discussion Span
Last Post by pritaeas
2

Why is it always you find the answer right after posting. I forgot to add:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

as mentioned on SO.

The XML I am sending looks like this now:

<ArrayOfItem><Item><Value>1</Value></Item><Item><Value>2</Value></Item></ArrayOfItem>

Can't remember where I read that it had to be specified as ArrayOfXxx

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.