14

I am returning the xml output to the browser with a wcf webservice, if a property of a DataContract is null, it still comes through in the response as

<Id i:nil="true" />

Is there a way to have it not return in the response at all?

Nick Allen
  • 11,970
  • 11
  • 45
  • 58

2 Answers2

25

Figured it out myself, I needed to add the following atrribute to my DataMembers

[DataMember(EmitDefaultValue = false)]
Nick Allen
  • 11,970
  • 11
  • 45
  • 58
  • 2
    This do not seem to work for me... anything else I should add? – Daniel Dolz May 16 '13 at 20:24
  • 3
    Is there not a way to specify this as the default behavior? This seems like a terrible oversight on Microsoft's part. For example, annotating every property of a class library, containing more than 100+ POCOs is simply not acceptable. – crush May 06 '14 at 17:01
  • 1
    @Crush: I agree. Anybody have a global solution for this instead of by property? – goku_da_master May 05 '15 at 14:57
0

In the class in which your property is present have a constructor. In the constructor initialize it as null. If a property of a DataContract is null, then it will not appear in your xml output. If you want it to appear it in your output when data is present in it just instantiate it again in your class where you expose the data.

Simran
  • 102
  • 1
  • 8