BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

XML
.NET 2.0+

XML Serialization of Arrays and Collections

Arrays and collections can be serialized to XML. The standard action when using the default serializer is for the name of the collection property to be added to the XML, with a contained element for each item named according to the items' data types.

Serializing Arrays and Collections

When you serialize an object that contains arrays or collections to XML, an element is created based upon the name of the property or field. This contains one child element for each item in the array. The names of the child elements match the data type names for the items. Sometimes you will want to modify this behaviour to change the element naming or to adjust other details, such as adding namespaces or controlling the serialization of different item data types.

This article describes how the serialization can be modified. We will use two test classes for this purpose. One class describes an employee of a business. The second represents a department within the company, which can include a number of employees in a simple generic list. The classes are shown below:

public class Department
{
    public string Name { get; set; }

    public List<Employee> Employees { get; set; }

    public Department()
    {
        Employees = new List<Employee>();
    }
}

public class Employee
{
    public string Name { get; set; }

    public Employee() { }

    public Employee(string name)
    {
        Name = name;
    }
}

The examples will all serialize a sample department and its employees using the code below. This will be the same for all samples. The only changes will be to attributes of the Department type.

Department dept = new Department();
dept.Name = "IT";
dept.Employees.Add(new Employee("Bob"));
dept.Employees.Add(new Employee("Jim"));
dept.Employees.Add(new Employee("Mel"));

XmlSerializer serializer = new XmlSerializer(dept.GetType());
using (StreamWriter writer = new StreamWriter(@"d:\Department.xml"))
{
    serializer.Serialize(writer, dept);
}

If you run the code shown above, the department is serialized with an "Employees" element. The name is from the name of the serialized property. The three employees are serialized as children of the Employees element, each under the name, "Employee", from the class name. The generated XML is shown below. The namespace and the xml element have been removed for readability.

<Department>
  <Name>IT</Name>
  <Employees>
    <Employee>
      <Name>Bob</Name>
    </Employee>
    <Employee>
      <Name>Jim</Name>
    </Employee>
    <Employee>
      <Name>Mel</Name>
    </Employee>
  </Employees>
</Department>

XmlArray

The first attribute we will consider is XmlArray. This can be applied to properties that are declared as arrays or collections. It modifies the outer XML element for the property. A common use for this attribute is modifying the name of the outer element. You can do this by passing the desired name between the attribute's parentheses.

In the Department class below, the XmlArray attribute has been applied to the Employees list. This changes the name of the generated collection element.

public class Department
{
    public string Name { get; set; }

    [XmlArray("Staff")]
    public List<Employee> Employees { get; set; }

    public Department()
    {
        Employees = new List<Employee>();
    }
}

After serialization you can see that the Employees element has been renamed to "Staff".

<Department>
  <Name>IT</Name>
  <Staff>
    <Employee>
      <Name>Bob</Name>
    </Employee>
    <Employee>
      <Name>Jim</Name>
    </Employee>
    <Employee>
      <Name>Mel</Name>
    </Employee>
  </Staff>
</Department>
10 July 2011