2

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;
}

enter image description here

It does just fine if the properties are in the defined order in the XML.

Jason Butera
  • 2,376
  • 3
  • 29
  • 46
  • 1
    I am also confused. Your code shows two string properties. When they are deserialized they are flattened into an object. There is no _order_ – maccettura Jun 28 '17 at 18:49
  • interesting... does it really behave like that? so the value of Prop1 would end up in Prop2 because of contradicting order, or does it just fail to deserialize? – Cee McSharpface Jun 28 '17 at 18:51
  • yeah that's what I think too, but then where's the point? – Cee McSharpface Jun 28 '17 at 18:55
  • 1
    Would two classes (one for serialization, another for deserialization) works as a solution ? – Alexei Levenkov Jun 28 '17 at 19:04
  • If for some reason it's critical for one property to be set before the other in deserialization, you could write custom serialization code, but it might be better to fix the issue that causes the requirement. – 15ee8f99-57ff-4f92-890c-b56153 Jun 28 '17 at 19:08
  • @gunr2171 It does NOT give the same result when the Order attributes are used. See my edit. – Jason Butera Jun 28 '17 at 19:20
  • @JasonButera I stand thoroughly corrected. https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.order%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – gunr2171 Jun 28 '17 at 19:21
  • @EdPlunkett I'm working against an industry "standard". We provide an outgoing XML feed as well as read other outside feeds. Our outgoing XML is adhering strictly to the standard with regards to order, however other outside implementations don't always (when it comes to the order). If I want the most flexibility, I'll probably have to do what Alexei suggests and use different classes (Which I don't really want to do) – Jason Butera Jun 28 '17 at 19:26
  • It might be an option to first transform the source xml using an xslt into a proper formatted/ordered xml file. – Emond Jun 28 '17 at 19:32
  • 1
    Is this a duplicate? [Deserializing XML with unknown element order](https://stackoverflow.com/a/33508815/3744182). – dbc Jun 28 '17 at 21:17

0 Answers0