Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to make XmlSerializer output xml in a defined order?

Currently I'm using XmlSerializer to serialize and deserialize an object. The xml is generated in an undefined order which is understandable but makes it annoying when comparing versions of the object, since the order of properties is different each time. So for instance I can't use a normal diff tool to see any differences.

Is there an easy way to generate my xml in the same order every time, without writing the ReadXml and WriteXml methods myself? I have a lot of properties on the class, and add new ones every now and again, so would prefer to not have to write and then maintain that code.

(C# .net 2.0)

like image 522
Rory Avatar asked Mar 04 '09 19:03

Rory


People also ask

What is the most flexible way of serialization to store objects in an open standards based document?

The simplest thing to do is mark your objects with the Serializable attribute and then use a binary formatter to handle the serialization. The entire class graph shouldn't be a problem provided that any contained objects are also marked as Serializable.

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

What is System XML serialization Xmlelementattribute?

XmlAttributes Class (System. Xml. Serialization) Represents a collection of attribute objects that control how the XmlSerializer serializes and deserializes an object.

Which annotation is needed for serialization and deserialization of XML format in the model class?

Jackson annotations are useful in defining and controlling the process of serialization and deserialization across various formats such as XML, JSON, and YAML.


2 Answers

The XmlElement attribute has an order property. You can use that as a start.

If you need to find the diff in Xml files, you might want to take a look at this.

like image 165
Szymon Rozga Avatar answered Oct 05 '22 04:10

Szymon Rozga


Decorate your properties with the XmlElementAttribute, setting the Order parameter.

like image 38
bdukes Avatar answered Oct 05 '22 03:10

bdukes