Skip to content
Yanos edited this page Dec 6, 2014 · 2 revisions

Serialization

The serializer can serialize only object (reference type), the System.Xml.Linq supporting value type serialization.

Tag name

If the tag name is not provided, XDocSerializer convert the type name to a tag name (removing some invalid characters).

Serialize a version without tag name :

var node = serializer.Serialize(new Version(1, 2, 3, 4));

produce

<Version>...</Version>

Serialize a version with tag name :

var node = serializer.Serialize(new Version(1, 2, 3, 4), "MyTag");

produce

<MyTag>...</MyTag>

If the type is a generic enumerable we use the generic type as name (see below for the list serialization and pluralized names).

Serialize a string array without tag name :

var node = serializer.Serialize(new String[] { "One", "Two" });

produce

<Strings>
  <String>One</String>
  <String>Two</String>
</Strings>

Serialize a string array with tag name pluralized :

var node = serializer.Serialize(new String[] { "One", "Two" }, "Items");

produce

<Items>
  <Item>One</Item>
  <Item>Two</Item>
</Items>

Serialize a string array with tag name singularized :

var node = serializer.Serialize(new String[] { "One", "Two" }, "Item");

produce

<Item>
  <Item>One</Item>
  <Item>Two</Item>
</Item>

In the object each public fields and properties name is used as tag name.

Serialize a version without tag name :

var node = serializer.Serialize(new Version(1, 2, 3, 4));

produce

<Version>
  <Major>1</Major>
  <Minor>2</Minor>
  <Build>3</Build>
  ...
</Version>

Object serialization

When serializing an object, each public fields and properties are serialized in a new XElement with the member name as tag name.

If the member is a value type, the value is setting in the XElement content (after a string conversion with the XDocSerializer.Culture).

If the member value is null, the member is not serialized.

With this class

public class Cls
{
    // Private fields
    private String PValue1 = "Test";
    // Public property        
    public String Value1 { get { return PValue1; } set { PValue1 = value; } }
    // Public field
    public Double Value2 = 7.2;
    // Read only public property
    public int Value3 { get { return 2; } }
    // Protected property
    protected String Value4 { get; set; }
    // Object property
    public Cls Value5 { get; set; }
}

Serialize an object with property class as null :

var node = serializer.Serialize(new Cls());

produce

<Cls>
  <Value2>7.2</Value2>
  <Value1>Test</Value1>
  <Value3>2</Value3>
</Cls>

Serialize an object with a property class :

var node = serializer.Serialize(new Cls() {
    Value5 = new Cls() {
        Value1 = "Property value",
        Value2 = 987
    }
});

produce

<Cls>
  <Value2>7.2</Value2>
  <Value1>Test</Value1>
  <Value3>2</Value3>
  <Value5>
    <Value2>987</Value2>
    <Value1>Property value</Value1>
    <Value3>2</Value3>
  </Value5>
</Cls>

List serialization

When the value is a list (Array, ou IEnumerable<>), each item contains is serialized in a XElement with the same tag name as the list XElement.

If the list XElement name is a plurial, the child XElement name is singularized.

Serialize a string array without tag name, convert type name to "Strings" :

var node = serializer.Serialize(new String[] { "One", "Two" });

produce

<Strings>
  <String>One</String>
  <String>Two</String>
</Strings>

Serialize a string array with a tag name pluralized :

var node = serializer.Serialize(new String[] { "One", "Two" }, "Items" );

produce

<Items>
  <Item>One</Item>
  <Item>Two</Item>
</Items>

Serialize a string array with a tag name singularized :

var node = serializer.Serialize(new String[] { "One", "Two" }, "Item" );

produce

<Item>
  <Item>One</Item>
  <Item>Two</Item>
</Item>

Dictionary

When the value is a IDictionary<String, Object>, each member is serialized with the key as tag name.

var dic = new Dictionary<String, Object>() {
    {"V1", 1234},
    {"V2", "Texte"},
    {"V3", new Cls() {
        Value5 = new Cls() {
            Value1 = "Property value",
            Value2 = 987
        }
    }}
};
var node = serializer.Serialize(dic, "Dic");

produce

<Dic>
  <V1>1234</V1>
  <V2>Texte</V2>
  <V3>
    <Value2>7.2</Value2>
    <Value1>Test</Value1>
    <Value3>2</Value3>
    <Value5>
      <Value2>987</Value2>
      <Value1>Property value</Value1>
      <Value3>2</Value3>
    </Value5>
  </V3>
</Dic>

Clone this wiki locally