Martijn
Martijn

Reputation: 24789

How to force XmlSerializer to generate an empty tag when a condition is met?

I have an XSD from another company which forces me to generate empty tags in the Xml. I've created a class based on this XSD with XSD.exe. One of the properties is a DateTime property. In my case I don't need this property, so I set it's value to DateTime.MinValue. This, offcourse, generates a tag with the min value as a value.

What I want is that while serializing a check takes place: if the value equals the min value, then generate an emptye tag, otherwise create the tag with the value.

Does someone know how to accomplish this?

Upvotes: 0

Views: 552

Answers (1)

Andrei V
Andrei V

Reputation: 7506

If you have the option of converting this property to string instead of storing it as a DateTime object, you can set it to String.Empty instead of DateTime.MinValue. The serializer will then create an empty tag.

Again, as far as I know, this only works with strings.

Upvotes: 1

Related Questions