开发者

DataContractSerializer on augmented list type

开发者 https://www.devze.com 2023-02-19 21:05 出处:网络
I am having trouble using DataContracts to deserialize where a list wrapper is augmented with additional data.The important part of the XML is pasted here, with a full link to the XSD and documentatio

I am having trouble using DataContracts to deserialize where a list wrapper is augmented with additional data. The important part of the XML is pasted here, with a full link to the XSD and documentation at the end of this post.

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

The point I am at now is:

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

Now the problem, when I deserialize this using the DataContractSerializer, I get the list as expected, only all of the values for Matches on SearchResultsCriteria comes through as 0. What am I missing to get the augmented list to deserialize correctly? I found this msdn article which helped me get through some other problems, but doesn't have anything regarding this specific issue.

EDIT: The order matters!

I came across this other msdn article about the order of the elements in the XML when using DataContract deserialization. So from what I can tell, it looks like the problem is that the order cannot be determined since there is a variable number of <criterion> elements above the criteria matches. I was hoping it would be the case that I could specify the order of the criteria matches to be 1, and when only 1 criteria was specified it would work, because I could live with that situation. However, that does not appear to work when the class is specified with CollectionDataContract as I have specified above.

For more specific context, I am trying to write a service wrapper for the thrustcurve.org api.


I'm not sure what you mean by "augmented list types" exactly, but I will assume you mean list type members that are not classified as DataMember attributes but that nonetheless need to be round-tripped back and forth during the serialization process.

Based on this understanding, what you need is IExtensibleDataObject. http://msdn.microsoft.com/en-us/library/ms731083.aspx and http://msdn.microsoft.com/en-us/library/ms731138.aspx and http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iextensibledataobject.aspx would be good starting points here.

If I misunderstood your question...sorry! Please do clarify what you meant.

0

精彩评论

暂无评论...
验证码 换一张
取 消