Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi can't deserialize XML into a list

I'm trying to POST a list or array of values that are automatically deserialized to a complex object called RejectModel. This works perfectly when receiving the data as JSON, but when sending XML data, the automatic serializer makes rejectionList NULL.

I have tried using the default Xml serializer instead of the dataContract serializer. This gives me the following error:

No MediaTypeFormatter is available to read an object of type 'List'1' from content with media type 'application/xml'.

I have tried changing the list to an Array with the same results.

I feel like I'm just not naming my XML containing element correctly. Any suggestions would be appreciated.


Post declaration. Accept a list of rejectModels.

public HttpResponseMessage Post(List<RejectModel> rejectionList)
        {
            if (rejectionList == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The request was empty, malformed, or did not include an array of values.");
            }
            else if (rejectionList.Rejections.Length == 0)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "An empty array was passed.");
            }
        }

Reject Model, I want to accept a list/array of these

[DataContract]
public class RejectModel : BaseModel
{
    [Required]
    [DataMember(IsRequired = true)]
    public int LeadId { get; set; }

    [DataMember]
    public int? PropertyId { get; set; }

    [DataMember]
    public string PartnerPropertyId { get; set; }

    [DataMember]
    public string OriginalSource { get; set; }

    [DataMember]
    public DateTime OriginalReferralDate { get; set; }
}

One of my many request attempts. I have tried many tag names for "rejectList". I include "Accept: application/xml" in the request header.

<?xml version="1.0" encoding="UTF-8"?>
<rejectList>
 <RejectModel>
<LeadId>10102085</LeadId>
<PropertyId>60278</PropertyId>
 </RejectModel>
</rejectList>
like image 741
caltrop Avatar asked Mar 22 '23 09:03

caltrop


1 Answers

Never fails. Struggle for two days, post a question, discover the answer in 30 minutes.

When using DataContractSerializer (the default xml serializer for WebAPI), you must include a namespace in your XML request and on the object's DataContract. You also need to name the xml collection "ArrayOf..." even if you're taking in a List. Here's an example following the structure of my original question:

[DataContract(Namespace = "http://blahblah.org/2013/Some.Namespace.You.Defined")]
public class RejectList{
    List<RejectModel> list{
        get;
        set;
    }
}

<ArrayOfRejectModel xmlns="http://blahblah.org/2013/Some.Namespace.You.Defined">
  <RejectModel> ... </RejectModel>
  <RejectModel> ... </RejectModel>
</ArrayOfRejectModel>

However, if you include a blank namespace with your DataContract decoration, you don't have to include a namespace inside the XML request.

[DataContract(Namespace = "")]
public class RejectList{
    List<RejectModel> list{
        get;
        set;
    }
}

<ArrayOfRejectModel>
  <RejectModel> ... </RejectModel>
  <RejectModel> ... </RejectModel>
</ArrayOfRejectModel>

Related reading:

Adjust MVC 4 WebApi XmlSerializer to lose the nameSpace

Remove namespace in XML from ASP.NET Web API

How to serialize xml into desired format in asp.net web api

like image 171
caltrop Avatar answered Apr 01 '23 12:04

caltrop