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

Controlling XML Serialization of Attributes

The default action, when serializing objects to XML, is for each public property and field to generate an XML element. The XmlAttribute attribute can be applied to public members to modify this behaviour and instead generate XML attributes.

XML Attributes

An XML attribute is a name / value pair that can be added to the opening tag of an XML element. It allows additional information to be included in an XML element. The following shows the opening tag of an element with two attributes. The attributes are named, "Name" and "Budget". Their values are provided in quotes.

<Department Name="IT" Budget="250000">

When you use the default settings for serializing objects to XML, the public properties and fields are represented as elements in the resultant XML. The object becomes a single element and each property is added as a child element. Where members with complex types are present, these may create nested elements.

Sometimes you may prefer that simple types are serialized as attributes of the object's root element. This can be achieved by decorating the properties with the XmlAttribute attribute. Members with complex types containing further properties cannot be serialized in this manner.

XmlAttribute

We will demonstrate the use of XmlAttribute by serializing an object to XML using a number of versions of the attribute. To begin, create the following class. This is a simplified version of the class used in the "Controlling Serialization of XML Elements" article. Review that article for the code that performs the serialization.

NB: The example class uses 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; }
}

If we create and serialize a Department object with the name, "IT" and a budget of 250,000, we get the following XML. NB: The xml tag and the default namespaces have been removed for clarity.

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

In this case the two properties have been serialized to XML elements.

Serializing a Property as an Attribute

The simplest way to use XML attributes for properties is to add the XmlAttribute attribute to the member with no parameters. For example, try changing the declaration of the Name property as follows:

public class Department
{
    [XmlAttribute]
    public string Name { get; set; }

    public decimal Budget { get; set; }
}

When serialized, the results are as shown below. Note that the Name property is now serialized as an attribute of the Department element. The Budget property is still represented by a child element.

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

You can apply the XmlAttribute to multiple properties, as long as they are simple items that will be serialized as single strings. Try adding the attribute to the Budget property.

public class Department
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlAttribute]
    public decimal Budget { get; set; }
}

As every member in the class is now configured to be an XML attribute, the resultant XML contains a self-closing Department tag with two attributes.

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

Setting Attribute Names

Sometimes you will want to change the name of the XML attribute so that the property name is not used. You can do so using the AttributeName parameter, which accepts a string containing the new name. In the example below the Name property's attribute has been renamed to "DepartmentName".

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

    public decimal Budget { get; set; }
}

When serialized, the new attribute name is included:

<Department DepartmentName="IT">
    <Budget>250000</Budget> 
</Department>

Specifying Attribute Namespaces

As with other parts of a serialized XML document, such as the root element or individual child elements, you can specify the namespace for each attribute. To do so, set the value of the Namespace parameter. When a namespace is applied to an attribute, a namespace prefix is required. The serializer automatically generates prefixes for each unique namespace used.

To demonstrate, modify the class to add a namespace, as follows:

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

    public decimal Budget { get; set; }
}

When serialized, the namespace is added to the root element and given a prefix automatically. In the XML below, the prefix is "d1p1". The attribute is modified to include the prefix to link it to the declared XML namespace.

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