3

I am xml serializing an object and I cannot seem to change the order in which the fields are serialized.

I.e.

public class foo{
string a {get;set;}
string b {get;set;}
}

Comes out as (pseudocode)

<foo b="world" a="hello" />

When I need

<foo a="hello" b="world" />

Can anyone help?

pravprab
  • 2,301
  • 3
  • 26
  • 43
maxp
  • 24,209
  • 39
  • 123
  • 201
  • 1
    in xml, both output are equivalent. Don't your consuming program is xml compliant ? Anyways, I don't think it's possible without a custom serialization process. It is for elements with the [Order property of XmlElementAttribute class](http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.order.aspx), and there is no equivalent for attributes – Steve B Dec 15 '11 at 09:49
  • @SteveB Unfortunately not, the remote program throws an exception if the order is incorrect. Its out of my hands. – maxp Dec 15 '11 at 09:59

1 Answers1

5

If you cannot achieve this via decorating the class with XML attributes (which you cannot do if you require XML attributes instead of XML elements as output), you may need to implement IXmlSerializable yourself, mentioned briefly here:

custom xml serialization

And here:

Proper way to implement IXmlSerializable?

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187