views:

61

answers:

1

Just wondering if anyone knows how the XmlSerializer determines the order for deserializing any given object's properties.

Added info:

I have a class like this:

    private bool _hasGaps = false;
    public bool HasGaps
    {
        get { return _hasGaps; }
        set
        {
            _hasGaps = value;
        }
    }

    private List<GapInfo> _gaps;
    public List<GapInfo> Gaps
    {
        get { return _gaps; }
        set
        {
            _gaps = value;
        }
    }

I set a breakpoint in both Gaps setter and HasGaps setter, Gaps gets deserialized first.

More info

Here's what the XML looks like:

<Tube Id="3df08765-c4e7-4a87-a0ed-ec76169b47af" Name="Tube">
  <HasGaps>false</HasGaps>
  <Gaps />
</Tube>
+2  A: 

It deserializes them in the order they appear in the class.

Did you have reason to believe it might use a different order?

John Saunders
Thanks for the suggestion, but that doesn't seem to be the case, see added info in my question.
Carlo
Added the xml sample
Carlo
BTW, what's your source for saying that it deserializes them in the order they appear? Maybe there's something there that could give me a clue.
Carlo
@Carlo: common sense. The XML appears in a given order. The properties appear in a given order. Nothing (no annotation) specifies the order. What order could possibly be used? Only one - the order of the properties must be the order of the elements.
John Saunders
So based on your last comment in my question, I assume you're now saying there is no way to know the order?
Carlo
The order is the order of the properties.
John Saunders
I disagree. The Gaps property is the last defined property in my class, and somehow it's the first one to get deserialized. I set a break point in the Gaps set accessor, and in there I check all the other properties and they're not set. Then I set another breakpoint on any other property and it gets set. I know it's the same object thanks to the GetHashCode() method. Thanks for the help anyway.
Carlo
If you never see the other property setter called, then `Gaps` isn't the _last_ property to be set, it's the _only_ property to be set. If you saw `Gaps` set, then `HasGaps`, then you'd be able to say that `Gaps` is first.
John Saunders
Yeah, that's just what happened. Gaps is set first, then HasGaps is set, then TimeCreated, Name, TimeModified, etc. But for some reeason, Gaps is always set first. How do I know this? With the breakpoint in the Gaps set accesor, I check in the Watch window for Name, which is = null, TimeCreated is DateTime.Min, TimeModified too. Then when I set a breakpoint on the Name set accessor, the value changes of course, and Gaps is already initialized as an empty list.
Carlo
No, no, no. Try an example with `Gaps` being an integer or something. You're seeing the difference between treatment of a list and treatment of a primitive type.
John Saunders
I'll do that in a moment. What is this difference I'm seeing?
Carlo
Ok, with primitive types it does deserialize in order, does this means that it doesn't deserialize the list first because it's not a primitive type? If so, it doesn't really deserialize in the order they appear in the class.
Carlo