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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.