我使用DataContract
s到反序列化,其中一個列表包裝與額外增加的數據有問題。 XML的重要部分粘貼在這裏,在本文後面有完整的XSD鏈接和文檔。的DataContractSerializer上增強列表類型
<criteria>
<criterion>
<name>common-name</name>
<value>J350</value>
<matches>3</matches>
</criterion>
<criterion>
<name>designation</name>
<value>J350</value>
<matches>8</matches>
</criterion>
<matches>2</matches> <!-- only shows results matching all criteria -->
</criteria>
我在現在的問題是:
[CollectionDataContract(Namespace = "TheNamespace")]
public partial class SearchResultsCriteria : List<SearchResultsCriteriaInfo>
{
[DataMember(Name = "matches", Order = 1)] // fail
public int Matches { get; set; }
}
[DataContract(Name = "criterion", Namespace = "TheNamespace")]
public partial class SearchResultsCriteriaInfo
{
[DataMember(Name = "name", Order = 0)]
public string Name { get; set; }
[DataMember(Name = "value", Order = 1)]
public string Value { get; set; }
[DataMember(Name = "matches", Order = 2)]
public int Matches { get; set; }
// all other properties which can show up in the xml
}
現在的問題,當我反序列化這個使用DataContractSerializer
,我得到的預期,只有所有的值的Matches
上SearchResultsCriteria
列表通過爲0. 我錯過了什麼讓增強列表正確反序列化?我發現this msdn article幫助我解決了一些其他問題,但沒有關於這個特定問題的任何信息。
編輯:爲了事宜!
我在使用DataContract
反序列化時遇到了有關XML中元素順序的this other msdn article。因此,從我所知道的情況看,問題似乎是訂單無法確定,因爲在標準匹配上方存在可變數量的<criterion>
元素。我希望能夠確定標準匹配的順序爲1,並且只有一個標準被指定時,它纔會起作用,因爲我可以忍受這種情況。但是,如果我按照上面的規定在CollectionDataContract
中指定課程,那麼這似乎不起作用。
對於更具體的方面,我試圖寫的thrustcurve.org api服務包裝。
我標誌着這個答案是正確的,因爲你可以用'IExtensibleDataObject'做到這一點,但它是不可能的POCO的對象,所以我結束了使用XML序列化我的項目來代替。 – 2011-04-27 14:39:39