Harald Coppoolse
Harald Coppoolse

Reputation: 30464

Ignore Property to be (XML) serialized

My class has some properties that I don't want to be (XML) serialized.

Example:

public class MyClass
{
    private TimeSpan executionTime = TimeSpan.FromSeconds(0.0);

    public TimeSpan ExecutionTime
    {
        get {return executionTime;}
        set {executionTime = value;}
    }

    public double ExecutionTimeSeconds
    {
        get {return executionTime.TotalSeconds;}
        set {executionTime = TimeSpan.FromSeconds(value);}
    }
}

Apart from that this is a bit of useless code, You don't want both methods to be (de)serialized. This problem occurs always when several public readable and writable properties affect each other.

How to prevent this?

Upvotes: 3

Views: 3419

Answers (1)

Harald Coppoolse
Harald Coppoolse

Reputation: 30464

There is a difference between binary serialization and xml serialization

  • Binary serialization (de)serializes all (public and non public) fields
  • standard XML serialization (de)serializes public readable and writable properties

Binary Serialization

Your example class wouldn't give a problem in binary serialization. It wouldn't even lead to problems if your class would have two fields that have a relation to each other:

class BinaraySerializationProblem
{
    private int x = 0;
    private int doubleX = 0;
    // etc. Methods that change x and doubleX
}

As long as you take care of the relation between x and doubleX, binary serialization wouldn't be a problem.

However sometimes you don't want some items to be serialized for efficiency reasons:

[Serializable]
class MyImage
{
    private string imageFileName = null;
    private BitMap loadedImage = null;
    // etc. Methods to change imageFileName and load the image
}

When serializing this, you probably don't want to save the bitmap, because it can be recreated by reading the file. In that case you'll add the attribute [NonSerialized] to the field (not the property!)

[Serializable]
class MyImage
{
    private string imageFileName = null;

    [NonSerialized]
    private BitMap loadedImage = null;
    // etc. Methods to change imageFileName and load the image
}

[Serializable] and [NonSerialized] can be found in namespace System

XML Serialization

When saving XML, all ToString() values of all public readable and writable attributes are serialized.

So in case of XML both the values of ExecutionTime and ExecutionTimeSeconds will be saved.

If you don't want a property to be serialized use the attribute [XmlIgnore]. Change your class as follows:

[Serializable]
public class MyClass
{
    private TimeSpan executionTime = TimeSpan.FromSeconds(0.0);

    [System.Xml.Serialization.XmlIgnore] 
    public TimeSpan ExecutionTime
    {
        get {return executionTime;}
        set {executionTime = value;}
    }

    public double ExecutionTimeSeconds
    {
        get {return executionTime.TotalSeconds;}
        set {executionTime = TimeSpan.FromSeconds(value);}
    }
}

This has the effect that the value of property ExecutionTime is not serialized, only the value of ExecutionTimeSeconds is.

By the way, I've noticed that class TimeSpan is not XML-serializable, so if you need to xml serialize a time span, it is necessary to add [XmlIgnore] the property and create an extra property to save the TimeSpan in a serializable format (seconds / msec / hours / etc).

Upvotes: 2

Related Questions