2

Is there any way to conditionally specify EmitDefaultValue at runtime for the DataContractSerializer in .NET? Are there any workarounds?

For example, I am curious whether I could use EmitDefaultValue=false for WCF serialization, but I would like to be able to use EmitDefaultValue=true for XMLs generated for external interfaces, such as print.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
sky-dev
  • 6,190
  • 2
  • 33
  • 32

1 Answers1

1

Well, two options you potentially have are to use a data contract surrogate or an IObjectReference. I think both of these are overkill for you, but if you really, really want to support this scenario on your end, maybe this is a sacrifice you're willing to make. Note that using them also has significant implications for performance, interoperability, exposure via schema, etc.

I also want to point out that what you're trying to do -- EmitDefaultValue of false -- is not recommended for a number of reasons, even though I know what the common motivations are. See Why is using [DataMember(EmitDefaultValue = false)] not recommended?

Community
  • 1
  • 1
krisragh MSFT
  • 1,908
  • 1
  • 13
  • 22
  • Interesting. I'll check out the IObjectReference. I had briefly looked into the Data Contract Surrogate. I tend to agree that using EmitDefaultValue = false is hiding some other design flaw such as an overly bloated object structure. In this situation the only external interface to these serialized objects is print. – sky-dev Jul 19 '12 at 20:06