Modus Operandi
Modus Operandi

Reputation: 641

DataContractSerializer deserialization fails completely if just one value is missing

Is there any way to prevent DataContractSerializer's Deserialize to ignore missing values and keep deserializing the rest of the data, instead of abandoning everything, throwing an Exception and returning NULL?

I'm actively building the applicaton, so naturally its objects get new fields added quite frequently, and the further along it gets, the more pain it is to have to re-enter all the data every time any object type gets an extra field.

Upvotes: 3

Views: 1662

Answers (1)

ipa
ipa

Reputation: 1536

You can use the IsRequired Property of the DataMember Attribute.

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute_properties.aspx

Example:

[DataContract]
public class Data
{
     [DataMember]
     public string Required { get; set; }

     [DataMember(IsRequired=false)]
     public string? NotRequired { get; set; }
}

Upvotes: 1

Related Questions