Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 927
heidgert Avatar asked Jul 22 '12 10:07

heidgert


3 Answers

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.

like image 153
SimperT Avatar answered Oct 16 '22 19:10

SimperT


class Program
{
    static void Main(string[] args)
    {
        HttpConfiguration config = new HttpConfiguration();

        DataContractSerializer productsSerializer = new DataContractSerializer(typeof(List<Product>), "Products", String.Empty);
        config.Formatters.XmlFormatter.SetSerializer(typeof(List<Product>), productsSerializer);

        config.Formatters.XmlFormatter.Indent = true;

        config.Formatters.XmlFormatter.WriteToStreamAsync(
            typeof(List<Product>),
            new List<Product> { new Product { Name = "Product1" }, new Product { Name = "Product2" } },
            Console.OpenStandardOutput(),
            null,
            null).Wait();

        Console.WriteLine();
    }
}

[DataContract(Namespace = "")]
public class Product
{
    [DataMember]
    public string Name { get; set; }
}
like image 38
RaghuRam Nadiminti Avatar answered Oct 16 '22 18:10

RaghuRam Nadiminti


As elusive said....

[XmlArray("Products")]
public List<Product> Get()
{
    return repository.GetProducts();
}

If you are having issues because it is a method, then try redefining it as a property since it takes no parameters, and probably makes more sense.

[XmlArray("Products")]
public List<Product> Products { 
  get {
    return repository.GetProducts();
  }
}

That will list all Product items inside an element element named 'Products'.

<Products>
  <Product>
    <Name>Product1</Name>
  </Product>
  <Product>
    <Name>Product2</Name>
  </Product>
</Products>

P.S. To rename the Product item tags you can use the [XmlArrayItem("Product")] attribute to do this. If you want to have a flat list (don't wrap them in 'Products'), you can use the [XmlElement("Product")] and that will list them without grouping them.

like image 1
Guy Park Avatar answered Oct 16 '22 18:10

Guy Park