Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order fields when serializing

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?

like image 375
maxp Avatar asked Jan 26 '26 07:01

maxp


1 Answers

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?

like image 190
Adam Houldsworth Avatar answered Jan 27 '26 19:01

Adam Houldsworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!