2013-07-16 23 views
1

序列化時,如何刪除列表中的根元素,我有這個類與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; } 
    } 
} 

這是在我服務的方法響應類型。

我結束了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> 
+0

爲什麼要刪除列表中的「root」元素?你想達到什麼目的?你想如何反序列化你的例子? – nemesv

+0

@nemesv,我試圖以消費服務期望的格式實現序列化到XML。 – CaffGeek

+0

因爲DataContractSeriliezer不是一個通用的序列化程序,所以您可能需要退回來使用XmlSerliazer將完整的自定義服務與Linq to XML ... – nemesv

回答

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 
} 

這樣你就不會在序列化的時候得到一個嵌套的屬性。