使用DataContracts进行序列化时如何删除列表的根元素 [英] How to remove root element of list when serializing with DataContracts

查看:55
本文介绍了使用DataContracts进行序列化时如何删除列表的根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这堂课

[DataContract]
public class InsertLoansResponse
{
    private ProcSummary _processingSummary;
    private List<InsertLoanResponse> _items;

    [DataMember]
    public List<InsertLoanResponse> InsertLoanResponses
    {
        get { return _items ?? (_items = new List<InsertLoanResponse>()); }
        set { _items = value; }
    }

    [DataMember]
    public ProcSummary ProcessingSummary
    {
        get { return _processingSummary ?? (_processingSummary = new ProcSummary()); }
        set { _processingSummary = value; }
    }

    public void Add(InsertLoanResponse localState)
    {
        InsertLoanResponses.Add(localState);
    }

    [DataContract]
    public class ProcSummary
    {
        [DataMember(Name = "Success")]
        public int SuccessCount { get; set; }

        [DataMember(Name = "Failure")]
        public int FailureCount { get; set; }
    }
}

这是我的服务中方法的响应类型.

It's the response type for a method in my service.

我最终得到的XML如下所示:

I end up with xml that looks like this:

<InsertLoansResponse>
    <InsertLoanResponses>
        <InsertLoanResponse>
        </InsertLoanResponse>
        <InsertLoanResponse>
        </InsertLoanResponse>
    </InsertLoanResponses>
    <ProcessingSummary>
        <Failure></Failure>
        <Success></Success>
    </ProcessingSummary>
<InsertLoansResponse>

但是我不希望复数InsertLoanResponses根节点,我希望它看起来像这样:

But I do not want the plural InsertLoanResponses root node, I want it to look like this:

<InsertLoansResponse>
    <InsertLoanResponse>
    </InsertLoanResponse>
    <InsertLoanResponse>
    </InsertLoanResponse>
    <ProcessingSummary>
        <Failure></Failure>
        <Success></Success>
    </ProcessingSummary>
<InsertLoansResponse>

推荐答案

也许更改类而不是序列化器.

Perhaps change your class rather than your serializer.

[DataContract]
public class InsertLoansResponse : List<InsertLoanResponse>
{
   private ProcSummary _processingSummary;
   private List<InsertLoanResponse> _items;

   // and remove the Add method, as this is now implicit to the class
}

这样,您在序列化时就不会获得嵌套属性.

This way you will not get a nested property when serializing.

这篇关于使用DataContracts进行序列化时如何删除列表的根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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