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 3.0+

Controlling Serialization of XML Elements

When serializing classes to XML, each public property and field value is transformed into an XML element. The name of the element matches the name of the property. The XmlElement attribute allows the names and formatting of XML tags to be modified.

XML Elements

An XML element generally consists of a start tag, an end tag and all of the information in between. This can include attributes within the opening tag and text or other child elements contained within the tags. In some cases a single, self-closing XML tag will define an element. The following is an example element:

<Department Name="IT">IT Department</Department>

When objects are serialized to XML using default settings, each public property and field with a non-null value is converted into an element within the XML's root element. The element's name matches the name of the property or field so that the XML can later be deserialized into a new object with the original values. Null properties are not converted to elements; they are simply omitted. Sometimes it is necessary to override this behaviour to achieve modified results.

In this article we will examine the XmlElement attribute, which provides control over the output of XML for properties and fields.

XmlElement

We can demonstrate the use of the XmlElement attribute by creating some sample objects, serializing them to disk and viewing the generated XML. To begin, we need a class to serialize. The following class represents a department within a business, with properties for the department name and budget.

NB: The examples use automatically implemented properties. If you are using an early version of .NET you should expand these to full declarations.

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

To serialize the class we will use types from the System.IO and System.Xml.Serialization namespaces:

using System.IO;
using System.Xml.Serialization;

We can now add the code that performs the serialization and outputs the XML to disk. Add the following code, editing the path and filename of the XML file as desired:

Department dept = new Department();
dept.Name = "IT";
dept.Budget = 250000M;

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

If you execute the code, the XML file will contain information similar to that shown below. For clarity, the opening xml tag and default namespaces have been removed.

<Department>
    <Name>IT</Name> 
    <Budget>250000</Budget> 
</Department>

Controlling Element Names

It is common to change the element names for individual properties. You can do so by decorating the appropriate member with the XmlElement attribute and the ElementName named parameter. The new name is provided as the parameter's value.

Modify the Department class as follows to give the two properties custom element names:

public class Department
{
    [XmlElement(ElementName = "DepartmentName")]
    public string Name { get; set; }

    [XmlElement(ElementName = "DepartmentBudget")]
    public decimal Budget { get; set; }
}

If you run the modified code, the XML has the new element names instead of the property names.

<Department>
    <DepartmentName>IT</DepartmentName> 
    <DepartmentBudget>250000</DepartmentBudget> 
</Department>

NB: The XmlElement attribute includes some parameterised constructors that give the same functionality. In this article we will concentrate on named parameters only.

Setting a Namespace

You can assign an XML namespace to each property in a class using the Namespace parameter. For example, the code below specifies a namespace for the Name element:

public class Department
{
    [XmlElement(Namespace="http://www.blackwasp.co.uk")]
    public string Name { get; set; }

    public decimal Budget { get; set; }
}

When serialized, the namespace is added to the element as an XML attribute:

<Department>
    <Name xmlns="http://www.blackwasp.co.uk">IT</Name> 
    <Budget>250000</Budget> 
</Department>
25 January 2011