views:

295

answers:

2

I have added some nullable value types to my serializable class. I perform a serialization using XmlSerializer but when the value is set to null, I get an empty node with xsi:nil="true". This is the correct behaviour as I have found here: http://msdn.microsoft.com/en-us/library/ybce7f69%28VS.80%29.aspx

Is there a way to switch off this option so that nothing is output when the value type is null?

A: 

Closing, I worked around it and just handled xsi:nil="true" instead.

Nat Ryall
+1  A: 

i've had the same problem.. here's one of the places i read about handling nullable value types while serializing to XML: http://stackoverflow(dot)com/questions/244953/serialize-a-nullable-int

they mention about using built-in patterns like creating additional properties for nullable value types. like for a property named public int? ABC you must either add either public bool ShouldSerializeABC() {return ABC.HasValue;} or public bool ABCSpecified { get { return ABC.HasValue; } }

i was only serializing to xml to send to a sql stored proc, so me too has avoided changing my classes. i'm doing a [not(@xsi:nil)] check on all the nullable elements in my .value() query. **

You've mentioned you "worked around it and just handled xsi:nil" can you tell me about your work around?

**

Chin
I simply put in an intermediary step to scan the XML after it was serialised and remove any `xsi:nil="true"` nodes.
Nat Ryall
roger that. thanks!
Chin