title: Serialize object to XML without empty elements and elements with empty children
body: <p>I need to serialize object into XML request, but the service, I am calling, do not accept empty elements.</p>
  
  <p>I generated contract classes from XSDs and the request structure looks like follows:</p>
  
  <pre><code>/* Generated contracts - simplified &amp; without XML attributes */
  public class Request
  {
      public Request()
      {
          filters = new RequestFilters();
          properties = new ResultProperties();
      }
  
      public RequestFilters filters { get; set; }
  
      public ResultProperties properties { get; set; }
  }
  
  public class ResultProperties
  {
      public ResultProperties()
      {
          props = new List&lt;string&gt;();
      }
      public List&lt;string&gt; props { get; set; }
  }
  
  public class RequestFilters
  {
      public RequestFilters()
      {
          ids = new LongFilter();
          names = new StringFilter();
      }
  
      public LongFilter ids { get; set; }
      public StringFilter names { get; set; }
  }
  
  public class LongFilter
  {
      public LongFilter()
      {
          values = new List&lt;long&gt;();
      }
  
      public List&lt;long&gt; values { get; set; }
  }
  
  public class StringFilter
  {
      public StringFilter()
      {
          values = new List&lt;string&gt;();
      }
  
      public List&lt;string&gt; values { get; set; }
  }
  </code></pre>
  
  <p>For the code generation I've used <em>xsd2code</em> and configured it to exclude empty collections from serialization. When creating object</p>
  
  <pre><code>Request request = new Request();
  
  ResultProperties rp = new ResultProperties();
  rp.props.Add("foo");
  rp.props.Add("bar");
  
  request.properties = rp;
  </code></pre>
  
  <p>I can serialize it into XML: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;Request xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;filters&gt;
      &lt;ids /&gt;
      &lt;names /&gt;
    &lt;/filters&gt;
    &lt;properties&gt;
      &lt;props&gt;
        &lt;string&gt;foo&lt;/string&gt;
        &lt;string&gt;bar&lt;/string&gt;
      &lt;/props&gt;
    &lt;/properties&gt;
  &lt;/Request&gt;
  </code></pre>
  
  <p>However the backend service complains about empty <code>&lt;ids /&gt;</code> and <code>&lt;names /&gt;</code> elements.</p>
  
  <p>Is there a preferred/any way to exclude those empty elements from serialized XML? </p>
  
  <p>If I manage to exclude <code>&lt;ids /&gt;</code> and <code>&lt;names /&gt;</code>, eventually I would like to get rid of <code>&lt;filters&gt;</code> element too. Thanks.</p>
  
  <p><strong>UPDATE:</strong> Added relevant part of XSD</p>
  
  <pre><code>&lt;xs:schema&gt;
    &lt;xs:element name="Request"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:sequence&gt;             
          &lt;xs:element name="filters" type="ouext:RequestFilters" minOccurs="0" /&gt;
          &lt;xs:element name="properties" type="ouext:ResultProperties" minOccurs="0" /&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="RequestFilters"&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="ids" type="filters:LongFilter" minOccurs="0" /&gt;
          &lt;xs:element name="names" type="filters:StringFilter" minOccurs="0" /&gt;
        &lt;xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="LongFilter"&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="values" type="filters:LongInFilter" minOccurs="0" /&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="LongInFilter"&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="value" type="xs:long" minOccurs="0" maxOccurs="20000" /&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="StringFilter"&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="values" type="filters:StringInFilter" minOccurs="0" /&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="StringInFilter"&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="value" type="xs:string" minOccurs="0" maxOccurs="20000" /&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="ResultProperties"&gt;
        &lt;xs:sequence&gt;
          &lt;xs:element name="props" type="filters:StringInFilter" minOccurs="0" maxOccurs="unbounded"&gt;
          &lt;/xs:element&gt;
        &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong> Serialization is done as follows</p>
  
  <pre><code>string xmlPayload = null;
  XmlSerializer serializer = new XmlSerializer(typeof(Request));
  using (StringWriter sw = new StringWriter())
  {
      using (XmlWriter xw = XmlWriter.Create(sw))
      {
          serializer.Serialize(xw, payload); // payload is initialized object of Request type
          xmlPayload = sw.ToString();
      }
  }
  </code></pre>