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

heidgert picture heidgert · Jul 22, 2012 · Viewed 7.5k times · Source

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?

Answer

SimperT picture SimperT · Mar 14, 2015

I know you are not fond of the wrapper idea but there is a solution that somewhat uses a wrapper but also uses the xml attributes which are very easy to work with. My refusal to using the following approach is the use of the old serializer.

public class Product
{
    [XmlAttribute( "id" )]
    public int Id
    {
        get;
        set;
    }
    [XmlAttribute( "name" )]
    public string Name
    {
        get;
        set;
    }
    [XmlAttribute( "quantity" )]
    public int Quantity
    {
        get;
        set;
    }
}
[XmlRoot( "Products" )]
public class Products
{
    [XmlAttribute( "nid" )]
    public int Id
    {
        get;
        set;
    }
    [XmlElement(ElementName = "Product")]
    public List<Product> AllProducts { get; set; }
}

Now your controller can just return Products like:

    public Products Get()
    {
        return new Products
        {
            AllProducts = new List<Product>
            {
                new Product {Id = 1, Name = "Product1", Quantity = 20},
                new Product {Id = 2, Name = "Product2", Quantity = 37},
                new Product {Id = 3, Name = "Product3", Quantity = 6},
                new Product {Id = 4, Name = "Product4", Quantity = 2},
                new Product {Id = 5, Name = "Product5", Quantity = 50},
            }
        };
    }

now you can specify the serializer in start-up like so:

    var productssXmlFormatter = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
    productssXmlFormatter.SetSerializer<Products>( new XmlSerializer( typeof( Products ) ) );

I know it is not the most ideal way as to having to specify the serializer and losing the flexability and convenience of EF and Linq. Or at least having to intervene rather than just returning IEnumerable<>.

I have to give credit to the following site as I first learned of this way from the site at: http://justthisguy.co.uk/outputting-custom-xml-net-web-api/

This will result in the following xml:

<Products nid="0">
    <Product id="1" name="Product1" quantity="20"/>
    <Product id="2" name="Product2" quantity="37"/>
    <Product id="3" name="Product3" quantity="6"/>
    <Product id="4" name="Product4" quantity="2"/>
    <Product id="5" name="Product5" quantity="50"/>
 </Products>

Please don't forget to look at the site listed.