views:

22

answers:

1

I'm deserializing a custom object from a file to an object in my app using the XmlSerializer. My issue is that I want a field in the object to default to "True" rather than "False" for a new property that doesn't exist in the file that I am deserializing from.

By default, .Net is assigning this value to be false because it doesn't exist in the file and I want it to default to True if it doesn't exist. I used the System.ComponentModel.DefaultValue(True) attribute on the field in the definition of the object, but that didn't work. Does anyone know how to do this?

A: 

You can set the value of the boolean in the empty constructor of your object directly. This way the deserializer will create the deserialized object with true in the boolean.

samy
That worked. Thanks samy!
OneSource