序列化時,如何刪除列表中的根元素,我有這個類與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>
爲什麼要刪除列表中的「root」元素?你想達到什麼目的?你想如何反序列化你的例子? – nemesv
@nemesv,我試圖以消費服務期望的格式實現序列化到XML。 – CaffGeek
因爲DataContractSeriliezer不是一個通用的序列化程序,所以您可能需要退回來使用XmlSerliazer將完整的自定義服務與Linq to XML ... – nemesv