修改序列化ASP.NET WebAPI对象中的xml数组元素名称 [英] Modify the xml array element name in serialized ASP.NET WebAPI object

查看:86
本文介绍了修改序列化ASP.NET WebAPI对象中的xml数组元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WebAPI控制器中返回对象列表时,我一直在努力输出自定义根xml元素.

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();
    }

呈现如下所示的xml输出:

which renders an xml output like this:

<ArrayOfProduct>
  <Product>
    <Name>Product1</Name>
  </Product>
  <Product>
    <Name>Product2</Name>
  </Product>
</ArrayOfProduct>

我想将< ArrayOfProduct> 更改为< Products> ,但是还没有找到一种方法.

I would like to change <ArrayOfProduct> to <Products> but haven't found a way of doing so.

我尝试了 DataContract DataMember 属性的不同变体,但无济于事.

I have tried different variations of the DataContract and DataMember attributes to no avail.

有人知道是否有一种方法可以完成我想做的事情,而无需将我的 List< Product> 对象包装到一个新类中并返回它?

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?

推荐答案

我知道您不喜欢包装器的想法,但是有一种解决方案在某种程度上使用了包装器,但也使用了非常易于使用的xml属性..我拒绝使用以下方法是使用旧的序列化器.

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 ) ) );

我知道,这不是指定序列化器并失去EF和Linq的灵活性和便利性的最理想方法.或至少必须进行干预,而不仅仅是返回IEnumerable<>.

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/

这将导致以下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.

这篇关于修改序列化ASP.NET WebAPI对象中的xml数组元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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