title: Remove xml element with xsi:nil="true" C# serialization
body: <p>I have an XML which has some values and at times there can be null values as shown below: I do not want the nodes with null listed at all in the XML! The elements are set <code>IsNullable = true</code> in the class.  Any suggestions as I have tried out many stuffs in Google.. nothing helped!  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Materials&gt;
    &lt;Material&gt;
      &lt;MaterialName&gt;ABC&lt;/MaterialName&gt;
      &lt;Weight Value="0.303"&gt;
        &lt;Weight_A xsi:nil="true" /&gt;
        &lt;Weight_B xsi:nil="true" /&gt;
      &lt;/Weight&gt;
      &lt;Density Value="800"&gt;
        &lt;Density_A xsi:nil="true" /&gt;
        &lt;Density_B xsi:nil="true" /&gt;
      &lt;/Density&gt;
      &lt;Volume Value="8771.427" /&gt;
    &lt;/Material&gt;
    &lt;Material&gt;
      &lt;MaterialName&gt;ABC&lt;/MaterialName&gt;
      &lt;Weight&gt;
        &lt;V5_Weight&gt;2.009&lt;/V5_Weight&gt;
        &lt;V6_Weight&gt;1.3318154561904&lt;/V6_Weight&gt;
      &lt;/Weight&gt;
      &lt;Density&gt;
        &lt;V5_density&gt;1000&lt;/V5_density&gt;
        &lt;V6_density&gt;663&lt;/V6_density&gt;
      &lt;/Density&gt;
      &lt;Volume Value="2008771.427" /&gt;
    &lt;/Material&gt;
  &lt;/Materials&gt;
  </code></pre>
  
  <p>The class structure is as below:</p>
  
  <pre><code>[XmlRoot(ElementName = "Weight")]
  public class Weight
  {
      [XmlElement(ElementName = "Weight_A", IsNullable = true)]
      public string Weight_A { get; set; }
  
      [XmlElement(ElementName = "Weight_B", IsNullable = true)]
      public string Weight_B { get; set; }
  
      [XmlAttribute(AttributeName = "Value")]
      public string Value { get; set; }
  
  }
  
  [XmlRoot(ElementName = "Density")]
  public class Density
  {
      [XmlElement(ElementName = "Density_A", IsNullable = true)]
      public string Density_A { get; set; }
      [XmlElement(ElementName = "Density_B", IsNullable = true)]
      public string Density_B { get; set; }
  
      [XmlAttribute(AttributeName = "Value")]
      public string Value { get; set; }
  }
  
  [XmlRoot(ElementName = "Volume")]
  public class Volume
  {
      [XmlElement(ElementName = "Volume_A")]
      public string Volume_A { get; set; }
      [XmlElement(ElementName = "Volume_B")]
      public string Volume_B { get; set; }
  
      [XmlAttribute(AttributeName = "Value")]
      public string Value { get; set; }
  }
  
  [XmlRoot(ElementName = "Material")]
  public class Material
  {
      [XmlElement(ElementName = "MaterialName")]
      public string MaterialName { get; set; }
      [XmlElement(ElementName = "Weight")]
      public Weight Weight { get; set; }
  
      [XmlElement(ElementName = "Density")]
      public Density Density { get; set; }
      [XmlElement(ElementName = "Volume")]
      public Volume Volume { get; set; }
  }
  
  [XmlRoot(ElementName = "Materials")]
  public class Materials
  {
      [XmlElement(ElementName = "Material")]
      public List&lt;Material&gt; Material { get; set; }
  }
  </code></pre>