Skip to content

Platform.Xml.Serialization quickstart

Maarten edited this page Jun 25, 2015 · 4 revisions

Platform.Xml.Serialization

Platform.Xml.Serialization can be used alternative to the built-in C# XmlSerializer. The built-in serializer lacks many options which are implemented in this serializer.

Attributes

There are six main attributes that define how a property is serialized

  • Add [XmlElement] to properties that should be serialized als an Element
    <ThisIsAnElement>value here</ThisIsAnElement>
  • Add [XmlAttribute] to properties that should be serialized as an Attribute
    <MyClass ThisIsAnAttribute="value here"/>
  • Add [XmlTextAttribute] to a property when it should be the inner value of an class.
    <MyClass>this is the innver value</MyClass>
  • Add [XmlExcludeAttribute] to a property if it does not have to be serialized
  • [XmlListElementAttribute]
    Need info for this attribute
  • [XmlDictionaryElementTypeAttribute]
    Need info for this attribute

These attributes can change the way how the property is serialized

  • [XmlDateTimeFormat]
    Can be used to specify how a DateTime object is serialized and serialized
  • [XmlTreatAsNullIfEmptyAttribute]
    Can be used to set the value of the property to NULL if the value equals empty
  • [XmlCDataAttribute]
    Will surround the value with DATA tags
  • [XMLFormatAttribute]
    Can be applied to every property and sets how the property is serialized. This will only apply to types that have implemented IFormattable
  • [XmlTypeSerializerTypeAttribute]
    Can be used to specify which serializer is used to serialize the property
  • [XmlSerializeEnumAsIntAttribute]
    Will serialize an Enum value as INT instead of its string representation
  • [XmlPolymorphicTypeAttribute]
    Need info for this attribute
  • [XmlVariableSubstitutionAttribute]
    Need info for this attribute
  • [XmlVerifyRuntimeTypeAttribute]
    Need info for this attribute

##Serializing Make sure your objects are decorated with the correct attributes. Afterwards you can use the following code to serialize your object.

var xmlString = XmlSerializer<YourObject>.New().SerializeToString(yourObject);

##Deserializing Make sure your objects are decorated with the correct attributes. Afterwards you can use the following code to deserialize your object.

var yourObject = XmlSerializer<YourObject>.New().Deserialize(xmlString);