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+

Ignoring Members During XML Serialization

XML serialisation is a useful way to persist objects using a clear text, human-readable solution. In some cases it is necessary to modify the default serialization and ignore some public members. This can be achieved using the XmlIgnore attribute.

XML Serialization

Standard XML serialization stores the values of the public properties and fields of a class within an XML document. Sometimes you will want to limit the properties that are serialized, removing one or more items from the final XML. You can do this with the XmlIgnore attribute.

XmlIgnore

When a public property or field is decorated with the XmlIgnore attribute it is excluded from serialization, so the generated XML does not include its value. It is also ignored during deserialization. If the property is not given a value by another means it will be defaulted when reconstructed.

You may decide to exclude an element from the XML because it is not needed. One reason is to work around the problems caused by types that cannot be serialized correctly. For example, the TimeSpan structure cannot be serialized to XML. We can demonstrate this with the following class, which represents an operation to be logged:

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 LoggedOperation
{
    public string Description { get; set; }
    public DateTime Started { get; set; }
    public TimeSpan Duration { get; set; }
}

To create a LoggedOperation instance and serialize it we can use the following code. You may wish to change the path of the generated file.

LoggedOperation log = new LoggedOperation();
log.Description = "Backup";
log.Started = new DateTime(2010, 12, 7);
log.Duration = TimeSpan.FromMinutes(75);

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

The above code generates an XML file containing the details of the object. The XML is similar to that shown below, although I have removed the XML namespaces for readability. You can see that the TimeSpan's value has not been recorded. The Duration element is empty. If you were to deserialize the XML into a new object you would find that the TimeSpan value had been zeroed.

<LoggedOperation>
    <Description>Backup</Description> 
    <Started>2010-12-07T00:00:00</Started> 
    <Duration /> 
</LoggedOperation>

We can work around the TimeSpan serialization problem by introducing an extra property. The DurationTicks property provides access to the number of ticks held in the TimeSpan. As this is a long value, it can be serialized:

public long DurationTicks
{
    get { return Duration.Ticks; }
    set { Duration = TimeSpan.FromTicks(value); }
}

With the new property added there is no need to include the Duration in the XML. We can use the XmlIgnore attribute to tell the serializer to omit it:

[XmlIgnore]
public TimeSpan Duration { get; set; }

If you run the updated code you will see that the XML includes the DurationTicks property but not the Duration element:

<LoggedOperation>
    <Description>Backup</Description> 
    <Started>2010-12-07T00:00:00</Started> 
    <DurationTicks>45000000000</DurationTicks> 
</LoggedOperation>
9 December 2010