Is it possible to ignore the defined element order using the XmlSerializer when deserializing? I'd like strict ordering when I serialize, but want relaxed rules on deserialization.
public class Foo()
{
[XmlElement("Prop1", Order = 1)]
public string Prop1 { get; set; }
[XmlElement("Prop2", Order = 2)]
public string Prop2 { get; set; }
}
UPDATE
For those who don't understand the issue, here is the behavior:
Given this xml:
<MyObject>
<Name>Test</Name>
<Foo>
<Prop2>prop 2</Prop2>
<Prop1>prop 1</Prop1>
</Foo>
</MyObject>
and these classes:
[XmlRoot("MyObject")]
public class MyObject
{
[XmlElement("Name", Order = 1)]
public string Name { get; set; }
[XmlElement("Foo", Order = 2)]
public Foo[] Foos { get; set; }
}
public class Foo
{
[XmlElement("Prop1", Order = 1)]
public string Prop1 { get; set; }
[XmlElement("Prop2", Order = 2)]
public string Prop2 { get; set; }
}
When deserialized, it fails to initialize Prop1:
var xmlSerializer = new XmlSerializer(typeof(MyObject));
MyObject myobject = null;
using (var reader = new StringReader(xml))
{
myobject = xmlSerializer.Deserialize(reader) as MyObject;
}
It does just fine if the properties are in the defined order in the XML.