Ok this is the 'cause'.
A City entity has a reference to a State entity. A State entity contains a collection of City objects. When XML is produced, it can be a City object in the State entity's City collection is already processed. The XML will then contain a <ProcessedObjectReference> tag. This tag is then used to create an object which is used to fill in the reference later on, after all xml nodes have been processed.
Now, when the deserialization code runs into a <ProcessedObjectReference> tag, it creates that object to later on be able to re-create the object, but it doesn't store the place where this object was located. This means that the entity which is re-created using the <ProcessedObjectReference > tag handler after all other nodes have been re-created, is added at the back of the list, not at the position it was found.
If the code should keep track of the order in which the entities are encountered, INCLUDING the <ProcessedObjectReference > tags, it would be able to insert the entities later on, instead of adding them.
Example:
City C1, belongs to State S1
S1 has also C1 and C2.
C3 belongs to State S2
C4 also belongs to S2.
Ordered, this means that in the collection, the order is C1, C2, C3, C4 and states are ordered: S1, S2.
Deserializing, It first sees C1. So that one is added. Then it sees S1, as a reference from C1 and starts to deserialize that one. The 'Cities' collection in S1 contains C1 and C2. C1 is already processed, C2 is new, so C2 is then processed. After that, the main collection is further processed as C1 and S1 are done. It then runs into C2, but that one is already processed (another <ProcessedObjectReference > tag) and it then runs into C3, finds S2, deserializes C4, and continues after that with the main collection, runs into C4's <ProcessedObjectReference > tag and is done.
At that moment, the collection order is: C1, C3 and 2 <ProcessedObjectReference> objects have to be processed, for C2 and C4. As said, these are added at the back, so teh final order is: C1, C3, C2, C4.
I can fix this, though that will be tomorrow (saturday).