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.

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
Why do you want to remove this attribute? Your XML is a schema instance, is it not?
Have you read this? asp.net/web-api/overview/formats-and-model-binding/… 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."
Yes, unfortunately, I have not been able to get anything to change.
Please provide an example. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.