XmlSerializer and nullable attributes

Barg picture Barg · Jul 14, 2010 · Viewed 24k times · Source

I have a class with numerous Nullable<T> properties which I want to be serializable to XML as attributes. This is apparently a no-no as they are considered 'complex types'. So, instead I implement the *Specified pattern, where I create an addition *Value and *Specified property as follows:

[XmlIgnore]
public int? Age
{
    get { return this.age; }
    set { this.age = value; }
}

[XmlAttribute("Age")]
public int AgeValue
{
    get { return this.age.Value; }
    set { this.age = value; }
}

[XmlIgnore]
public bool AgeValueSpecified
{
    get { return this.age.HasValue; }
}

Which works fine - if the 'Age' property has a value, it is serialized as an attribute. If it doesn't have a value, it isn't serialized.

The problem is that, as I mentioned, a have a lot of Nullable-s in my class and this pattern is just making things messy and unmanageable.

I'm hoping there is a way to make Nullable more XmlSerializer friendly. Or, failing that, a way to create a Nullable replacement which is.

Does anyone have any ideas how I could do this?

Thanks.

Answer

Curt picture Curt · Jul 15, 2010

I had a similar problem with some code I was working on, and I decided just to use a string for the property I was serializing and deserializing. I ended up with something like this:

[XmlAttribute("Age")]
public string Age
{
    get 
    { 
        if (this.age.HasValue)
            return this.age.Value.ToString(); 
        else
            return null;
    }
    set 
    { 
        if (value != null)
            this.age = int.Parse(value);
        else
            this.age = null;
    }
}

[XmlIgnore]
public int? age;