How to specify the order of XmlAttributes, using XmlSerializer

You don't, as attributes have no order in XML (section 3.1 of the XML recommendation says: "Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.").


From my experience, the order of serialization of attributes is the same as the order you define your public properties. However, if you combine properties with fields in the same class, e.g.

[Serializable()]
public class MyClass
{
   [XmlAttribute("ADoubleProp")]
   public double ADoubleProp { get; set; }

   [XmlAttribute("AnIntField")]
   public int AnIntField = 42;
}

then the fields get written firsts as attributes and then the properties. The code above will produce something like this

<MyClass AnIntField="42" ADoubleProp="0" />