2

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.

ChrisBint
  • 12,773
  • 6
  • 40
  • 62

1 Answers1

3

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

JackPoint
  • 4,031
  • 1
  • 30
  • 42
danludwig
  • 46,965
  • 25
  • 159
  • 237
  • Close. Updating the CollectionDataContract and disabling serializer removes the xmlns:xsd tag, but not the xmlns:xsi Tag. I have updated the question with that code as well. – ChrisBint Oct 25 '12 at 17:15
  • Why do you want to remove this attribute? Your XML is a schema instance, is it not? – danludwig Oct 25 '12 at 17:39
  • 1
    Have you read this? http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization You may need to use the XmlSerializer. From the page: "The XmlSerializer class supports a narrower set of types than DataContractSerializer, but gives more control over the resulting XML. Consider using XmlSerializer if you need to match an existing XML schema." – danludwig Oct 25 '12 at 18:03
  • Yes, unfortunately, I have not been able to get anything to change. – ChrisBint Oct 25 '12 at 18:20
  • Please provide an example. :) – Christopher Bonitz Mar 08 '17 at 08:52