卸下MVC的Web API响应XML命名空间属性 [英] Remove XML namespace attributes from MVC Web API response

查看:179
本文介绍了卸下MVC的Web API响应XML命名空间属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从MVC的Web API下列响应;

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>

我想删除的xmlns:*标记

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.

我曾尝试;

[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; }
    }

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

这些没有任何效果。

These have no effect.

推荐答案

而不是 DataContract 中,使用 Col​​lectionDataContract ,没有启用的XmlSerializer

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

这篇关于卸下MVC的Web API响应XML命名空间属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆