Basic Serialization

How to make a class serializable?

This is easy. Simple mark the class as implementing IXPathSerializable interface. This is an empty interface, so you don't have to implement any methods. But you do so, you will be able to use extension methods from Artemkv.Transformation.XmlToObject namespace.

public class Person : IXPathSerializable
{
	public string FirstName { get; set; }
	public string LastName { get; set; }
}

How do I serialize the class to Xml?

Use ToXml method from Artemkv.Transformation.XmlToObject namespace.

using Artemkv.Transformation.XmlToObject;

...
	Person person = new Person()
	{
		FirstName = "Hugh",
		LastName = "Laurie"
	};

	string xml = person.ToXml();

By default, all the public fields and properties are serialized. Since we didn't specify any mapping rules, the Xml structure will mirror the class structure.

<Person>
  <FirstName>Hugh</FirstName>
  <LastName>Laurie</LastName>
</Person>

How do I deserialize the class from Xml?

Use LoadFromXml method from Artemkv.Transformation.XmlToObject namespace.

using Artemkv.Transformation.XmlToObject;

...

	string xml = @"<Person>
  <FirstName>Hugh</FirstName>
  <LastName>Laurie</LastName>
</Person>";

	Person person = XmlSerialization.LoadFromXml<Person>(xml);

Of course, this is the simplest example, and you can do more interesting things.

What types are supported?

Please refer to the table below to find out information about supported types. Note that all primitive types are supported, and some more complex types might require additional efforts.

Category Types Nullable Array
String System.String System.String? System.String[]
Char System.Char System.Char? System.Char[]
Integer System.Byte, System.SByte, System.Int16, System.UInt16, System.Int32, System.UInt32, System.Int64, System.UInt64 System.Byte?, System.SByte?, System.Int16?, System.UInt16?, System.Int32?, System.UInt32?, System.Int64?, System.UInt64? System.Byte[], System.SByte[], System.Int16[], System.UInt16[], System.Int32[], System.UInt32[], System.Int64[], System.UInt64[]
Float System.Single, System.Double, System.Decimal System.Single?, System.Double?, System.Decimal? System.Single[], System.Double[], System.Decimal[]
Boolean System.Boolean System.Boolean? System.Boolean[]
Date and time System.DateTime System.DateTime? System.DateTime[]
Guid System.Guid System.Guid? System.Guid[]
Base64 encoded string System.Byte[] using SerializeAsBase64Attribute N/A N/A
Enum Using EnumStringValueConverter or EnumNumberValueConverter Using EnumStringValueConverter or EnumNumberValueConverter Using EnumStringValueConverter or EnumNumberValueConverter
Flags Using EnumStringValueConverter or EnumNumberValueConverter Using EnumStringValueConverter or EnumNumberValueConverter Using EnumStringValueConverter or EnumNumberValueConverter
Object IXPathSerializable IXPathSerializable IXPathSerializable[]
Other Using ValueConverter Using ValueConverter Using ValueConverter

What is default serialization behavior?

By default, serialization uses class/member names to create an Xml. As a result, you will get an Xml that mirrors the class structure.

For example, this class:

public class Address : IXPathSerializable
{
	public string Street { get; set; }
	public string City { get; set; }
	public string ZipCode { get; set; }
	public string Country { get; set; }
}

public class Employee : IXPathSerializable
{
	public string FirstName { get; set; }
	public string LastName { get; set; }
	public DateTime DateOfBirth { get; set; }
	public bool IsMale { get; set; }
	public Decimal Salary { get; set; }
	public Address HomeAddress { get; set; }
	public string[] Skills { get; set; }
}

Will be serialized to:

<Employee>
  <FirstName>Hugh</FirstName>
  <LastName>Laurie</LastName>
  <DateOfBirth>1959-06-11T00:00:00.0000000</DateOfBirth>
  <IsMale>True</IsMale>
  <Salary>5000000.00</Salary>
  <HomeAddress>
    <Address>
      <Street>Hamilton Hodell</Street>
      <City>London</City>
      <ZipCode>W1W 8SR</ZipCode>
      <Country>UK</Country>
    </Address>
  </HomeAddress>
  <Skills>
    <Item>actor</Item>
    <Item>voice artist</Item>
    <Item>comedian</Item>
    <Item>writer</Item>
    <Item>musician</Item>
    <Item>recording artist</Item>
    <Item>director</Item>
  </Skills>
</Employee>

Read next: Controlling Xml Structure: Elements and Attributes

.

Last edited Nov 20, 2012 at 10:12 PM by artemkv, version 21

Comments

No comments yet.