views:

16

answers:

1

I have a WCF client that needs to generate a request containing this XML fragment :

<reason xsi:nil="true" nullFlavor="NA" typeCode="RSON" />

The schema is determined by the server and isn't under my control. The generated proxy code has a class for the reason element containing (among other things) properties nullFlavor and typeCode. The proxy uses the XmlSerializer.

How can I generate such a fragment? The XmlSerializer emits the xsi:nil attribute only if the corresponding member is null. And if it's null, it can't very well have properties that will be emitted as attributes!

BTW, the fragment is legal according to the XML Schema Instance spec, which says a nil element cannot contain any child elements or inner text, but may contain attributes.

A: 

This is a known limitation of the XmlSerializer. You may be able to get around it with some clever use of the IXmlSerializable interface and emitting the XML manually - unfortunately there isn't a clean solution that I know of.

Eugene Osovetsky