.NET, Why must I use *Specified property to force serialization? Is there a way to not do this?

Viewed 13865

I am using xml-serialization in my project to serialize and deserialize objects based on an xml schema. I used the xsd tool to create classes to use when serializing / deserializing the objects.

When I go to serialize the object before sending, I am forced to set the *Specified property to true in order to force the serializer to serialize all propeties that are not of type string.

Is there a way to force the serialization of all properties without having to set the *Specified property to true?

5 Answers

I faced same issue and ended up setting all *Specified properties to true by reflection.

Like

var customer = new Customer();
foreach (var propertyInfo in typeof(Customer).GetProperties().Where(p => p.Name.EndsWith("Specified")))
{
    propertyInfo.SetValue(customer, true);
}
Related