I read several threads on serialization I found here, but they did not answer my question.
I need a simple serialization library, that would reflect over the properties marked with certain attribute and store their values as a string. All the properties are either directly strings or directly convertable to and from string (built-in value types, bool, etc.). It should also understand simple containers (like generic lists) and serialize those content.
The control I need over the serialization is to indicate what properties are to be serialized (only those decorated with an attribute) and I also must be able to indicate that certain properties are serialized as the last ones: during serialization they must be also deserialized as the last ones.
The format does not matter, just normal param1=val1, param2=val2 would do, JSON would also do, as well as XML or even escaped or encoded binary format. Important is that I can have all my settings in one string I can easily store and load in my application.
Any ideas?
The
DataContractSerializerdoes that – you decorate the classes to be serialized with[DataContract], and only the members which you want serialized with[DataMember]. You can also control the order of the serialization via theOrderproperty of the DataMember attribute.The DataContractSerializer (DCS) always serializes the objects as XML, but the XML it uses can be both the “normal” XML with tags, or a more compact binary format, depending on the XmlWriter you pass to it during serialization (and XmlReader during deserialization). Another option which gives you more control over the produced XML (but it’s not as performant) is the
XmlSerializerclass, which has its own attributes for controlling the serialization.The code below shows an example of a DCS-serializable type and its serialization: