Monday, 24 November 2014

Xml Element Position in C# XML Serialization



I'm trying to serialize a class like this to an xml with the XmlSerializer class:



public class Car {
public InsuranceData Insurance { get; set; } // InsuranceData is a class with many properties
public int Person Owner { get; set; }
public int Age { get; set; }
public string Model { get; set; }

// lots of other properties...
}


I'd like to have the Insurance property at the very end of the xml document:



<Car>
...
<Insurance>
...
</Insurance>
</Car>


I need to do this because the server that processes the xml only works properly in this layout (and I cannot change the server code). I tried moving the property to the end of the class, but it makes no difference and I haven't found any attributes related to the serialization that would help. I could solve this by manipulating the xml as string, but I would prefer a more elegant solution. The objects has plenty of properties, so I don't want to create the xml string manually.


No comments:

Post a Comment