Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a property

 [XmlElement]
 public string[] Emails { get; set; }

which is initialized as string[0] at constructor.

If I XML serialize and deserialize a default instance of this object, the property is NULL.

How can I tell the XML Serializer to use an empty array instead of NULL for this property?

share|improve this question
1  
You can always implement the {get; set;} so if null is ever set (or attempts to return null in the getter) it instead uses an empty array. EDIT: Just be sure if you do the check on the get to also store the empty array in the backing field rather than just returning a new instance. – Chris Sinclair yesterday
@ChrisSinclair: I have hundred of such properties, is there a way without manual properties? – Herman Schoenfeld yesterday
Not that I know of. You can create a separate utility class that will (likely via reflection) find all arrays (and collections?) that are null and set them to a new instance of their appropriate empty collection. Have it run through this utility when calling your central deserialization method. – Chris Sinclair yesterday

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.