Is the order of items in a List preserved when des

2019-03-01 16:08发布

问题:

I am using XmlSerializer to deserialize an XML string into a Generic List. The deserialization works fine. However, I just want to make sure if the order of the items in the XML string will be maintained in the resulting Generic List. i.e. with respect to siblings in the XML string. i.e.

<Items>
 <Item>A</Item>
 <Item>B</Item>
</Items>

"A" will always be the first item in the List and "B" always the second. Didn't find any articles or information on the web about the order of the resulting deserialized XML elements?

回答1:

Simply put, yes. The order of elements is significant in XML, and is therefore preserved when deserializing.

Additionally, since the order of items in a list or array is significant, it must be preserved in order for XML serialization to be meaningful.