Modify the xml array element name in serialized ASP.NET WebAPI object

Viewed 7688

I have been struggling with outputting a custom root xml element when returning a list of objects in my WebAPI controller.

My controller method looks something like this:

    public List<Product> Get()
    {
        return repository.GetProducts();
    }

which renders an xml output like this:

<ArrayOfProduct>
  <Product>
    <Name>Product1</Name>
  </Product>
  <Product>
    <Name>Product2</Name>
  </Product>
</ArrayOfProduct>

I would like to change <ArrayOfProduct> to <Products> but haven't found a way of doing so.

I have tried different variations of the DataContract and DataMember attributes to no avail.

Does anyone know if there is a way of doing what I want, short of wrapping my List<Product> object in a new class and returning that instead?

8 Answers
Related