title: Prevent serialization of null members in DataContractSerializer
body: <p>Let me preface by saying I'm fairly new to WCF, and might be using the wrong terminology throughout here. My project has two components:</p>
  
  <ol>
  <li>A DLL containing the classes for Attachment, Extension, ReportType1, and ReportType2</li>
  <li>A WCF ServiceContract with an OperationContract as described below that deserializes as XML document into the relevant objects, then serializes it again as either JSON or XML back out to the client.</li>
  </ol>
  
  <p>I have an XML schema that looks like the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="windows-1252"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"&gt;
      &lt;xsd:element name="Attachment"&gt;
          &lt;xsd:complexType&gt;
              &lt;xsd:all&gt;
                  &lt;xsd:element name="Extension" type="Extension" minOccurs="0" /&gt;
              &lt;/xsd:all&gt;
          &lt;/xsd:complexType&gt;
      &lt;/xsd:element&gt;
      &lt;xsd:complexType&gt;
          &lt;xsd:sequence name="Extension"&gt;
              &lt;xsd:any processContents="skip" /&gt;
          &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>Following this schema, I have an XML document of the following type:</p>
  
  <pre><code>&lt;Attachment&gt;
      &lt;Extension&gt;
          &lt;ReportType1&gt;
              &lt;Name&gt;Report Type 1 Example&lt;/Name&gt;
          &lt;/ReportType1&gt;
      &lt;/Extension&gt;
  &lt;/Attachment&gt;
  </code></pre>
  
  <p>I've got the following classes in a compiled DLL:</p>
  
  <pre><code>public class Attachment
  {
      public Extension Extension { get; set; }
  }
  
  public class Extension
  {
      [XmlElement(ElementName = "ReportType1", IsNullable = false)]
      public ReportType1 ReportType1 { get; set; }
  
      [XmlElement(ElementName = "ReportType2", IsNullable = false)]
      public ReportType2 ReportType2 { get; set; }
  }
  </code></pre>
  
  <p>My WCF service deserializes the XML document into the above objects, and then returns it in JSON format via the following OperationContract:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
  Attachment Search();
  </code></pre>
  
  <p><strong>Actual Output as JSON</strong></p>
  
  <pre><code>{
      'Attachment': {
          'Extension': {
              'ReportType1': { ... },
              'ReportType2': null
          }
      }
  }
  </code></pre>
  
  <p><strong>Actual Output as XML</strong></p>
  
  <pre><code>&lt;Attachment&gt;
      &lt;Extension&gt;
          &lt;ReportType1&gt;...&lt;/ReportType1&gt;
          &lt;ReportType2 i:nil="true"&gt;&lt;/ReportType2&gt;
      &lt;/Extension&gt;
  &lt;/Attachment&gt;
  </code></pre>
  
  <p><strong>Desired Output as JSON</strong></p>
  
  <pre><code>{
      'Attachment': {
          'Extension': {
              'ReportType1': { ... }
          }
      }
  }
  </code></pre>
  
  <p><strong>Desired Output as XML</strong></p>
  
  <pre><code>&lt;Attachment&gt;
      &lt;Extension&gt;
          &lt;ReportType1&gt;...&lt;/ReportType1&gt;
      &lt;/Extension&gt;
  &lt;/Attachment&gt;
  </code></pre>
  
  <p>The classes from the DLL do not have the <code>DataContract</code> attribute, but seem to serialize just fine when being sent back from my <code>OperationContract</code>, as I get the above results.</p>
  
  <p>How can I tell it not to serialize the elements to JSON/XML if they are null without having the ability to turn the classes from the DLL into a <code>DataContract</code> class? Should I inherit the classes from the DLL, and somehow override them as <code>DataContract</code>? If so, how could I set attributes on the existing members of the base classes then?</p>
  
  <p>Please let me know if more information is required, and I will do my best to supply it.</p>