You you are keen on the order of the attributes, then the IXmlSerializable
interface will give you control of the serialization/deserialization process of the class. The order of attributes is determined by the order of the lines of code:
public void WriteXml(XmlWriter writer)
{
//First example xml element
writer.WriteStartElement("Item1");
writer.WriteAttributeString("Name", Name);
writer.WriteAttributeString("Price", Price);
writer.WriteAttributeString("Id", Id);
writer.WriteEndElement();
//Second example xml element
writer.WriteStartElement("Item2");
writer.WriteAttributeString("Price", Price);
writer.WriteAttributeString("Id", Id);
writer.WriteAttributeString("Name", Name);
writer.WriteEndElement();
}
outputs:
<Item1 Name="x" Price="y" Id="z">
<Item2 Price="y" Id="z" Name="x">
As you see, if you switch around lines of code, then the order is changed.
But beware, implementing this interface overwrites the default process, letting it be up to you to write the entire serialization/deserialization process.
Regards
Nautious