Kinexus
Kinexus

Reputation: 12904

Remove XML namespace attributes from MVC Web API response

I am getting the following response from an MVC Web Api;

<Products xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="my.namespace.com">
<Product>
<Id>1</Id>
<Name>Tomato Soup</Name>
<Category>Groceries</Category>
<Price>1</Price>
</Product>
<Product>
<Id>2</Id>
<Name>Yo-yo</Name>
<Category>Toys</Category>
<Price>3.75</Price>
</Product>
<Product>
<Id>3</Id>
<Name>Hammer</Name>
<Category>Hardware</Category>
<Price>16.99</Price>
</Product>
</Products>

I would like to remove the xmlns:* tags.

I have found various posts, including some on SO that give options, but these do not appear to work.

What I have tried;

[XmlRoot("Products",Namespace = "my.namespace.com")]
    [DataContract(Namespace = "")]
    public class ProductsModel : List<Product>
    {
    }

[XmlRoot("Product")]
    [DataContract(Namespace = "", Name = "Product")]
    public class Product 
    {
        [DataMember]
        public int Id { get; set; }

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

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

        [DataMember]
        public decimal Price { get; set; }
    }

and

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

These have no effect.

Upvotes: 2

Views: 5061

Answers (1)

danludwig
danludwig

Reputation: 47375

Instead of DataContract, use CollectionDataContract, without enabling the XmlSerializer.

Upvotes: 3

Related Questions