I have xml (which I have no control over) which I needwant to deserialize with the XmlSerializer. My trouble is that I need to preserve the order among two different tags in a single list. Let me give an example:
<users>
<luser>..some more elements..</luser>
<luser>..some more elements..</luser>
<admin>..different elements than in luser</admin>
<luser>..some more elements..</luser>
<admin>..different elements than in luser</admin>
<luser>..some more elements..</luser>
<admin>..different elements than in luser</admin>
<users>
Now, if I deserialze this the straight forward way, I end up with two lists, one for lusers and one for admins. However, the order they appear in is the implicit ID! (Which of course should have been an attribute or element in the xml but alas it is not)
I’ve tried to make a meta element like this
[XmlElement("luser"),XmlElement("admin")]
public List<Person> Person { get; set; }
with
public class Person
{
public Luser Luser { get; set; }
public Admin Admin { get; set; }
}
where the plan was to check the list to see if the entry a luser or an admin, and have the people wrapper maintain the order of the elements. However multiple XmlElement attributes on a single property seems to be illegal.
Another acceptable solution would be to end up with two lists but where each entry in each list had an ID I could use to get the original order.
I want this to be as clean as possible. Searching through the xml afterwards to find the order, manually extracting the tags, and similar, is something I would avoid and do as a very last resort.
Then make it an Attribute as soon as you can. Then work with the result.