Home All Groups Group Topic Archive Search About

Xml serializer and nullable types

Author
21 Jun 2006 2:26 PM
Per Millard
Using nullable types with the XmlSerializer doesn't seem to work properly when
deserialing an object containing a propertynameSpecified for the nullable
type.
The propertynameSpecified flag simply doesn't get set by the deserializer if
the nullable type property contains a value. Can anybody explain this ?
The following example demonstrates the problem

XmlSerializer xs = new XmlSerializer(typeof(valueclass));
MemoryStream ms = new MemoryStream();
valueclass c = new valueclass();
c.NullableValue = 10;
c.NullableValueSpecified = true;           
xs.Serialize(ms, c);
ms.Position = 0;
c = (valueclass)xs.Deserialize(ms);
//c.NullableValueSpecified should equal true but equals false
MessageBox.Show(c.NullableValue.ToString()+"
"+c.NullableValueSpecified.ToString());

public class valueclass
    {
        public int? NullableValue;
        [System.Xml.Serialization.XmlIgnore()]
        public bool NullableValueSpecified;
    }

regards

Per Millard
Systems architect

AddThis Social Bookmark Button