2

我有这门课

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

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

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

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

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

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

2 回答 2

1

也许改变你的类而不是你的序列化器。

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

这样,序列化时您将不会获得嵌套属性。

于 2014-06-16T13:49:33.833 回答
0

您可能需要为此操作定义自定义 MessageContract。查看以下链接,特别是“在消息合同中使用数组”部分和 MessageHeaderArrayAttribute。

http://msdn.microsoft.com/en-us/library/ms730255.aspx

于 2013-07-17T03:36:11.853 回答