title: Deserialize XML element with xsi:nil="true" in C#
body: <p>I'm trying to deserialize a XML file with <code>XmlSerializer</code> in C#.</p>
  
  <p>The destination class that follows was automatically generated using the xsd utility. </p>
  
  <pre><code>    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
      [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
      public partial class location
      {
  
          private string cityField;
  
          private string countryField;
  
          private string stateField;
  
          private string textField;
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlAttributeAttribute()]
          public string city
          {
              get
              {
                  return this.cityField;
              }
              set
              {
                  this.cityField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlAttributeAttribute()]
          public string country
          {
              get
              {
                  return this.countryField;
              }
              set
              {
                  this.countryField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlAttributeAttribute()]
          public string state
          {
              get
              {
                  return this.stateField;
              }
              set
              {
                  this.stateField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlTextAttribute()]
          public string Text
          {
              get
              {
                  return this.textField;
              }
              set
              {
                  this.textField = value;
              }
          }
      }
  </code></pre>
  
  <p>Everything works fine until I reach this portion of the file:</p>
  
  <pre><code>&lt;locations&gt;
      &lt;location country="PARAGUAY" city="Ciudad del Este" state="Alto Parana" xsi:nil="true"/&gt;
      &lt;location country="BRAZIL" city="Passo Fundo" state="Rio Grande do Sul" xsi:nil="true"/&gt;
  &lt;/locations&gt;
  </code></pre>
  
  <p>As <a href="https://msdn.microsoft.com/en-us/library/ybce7f69(v=vs.100).aspx#Anchor_2" rel="nofollow" title="MSDN">stated in the MSDN</a>, an element with xsi:nil="true" will be deserialized as a null object, losing all attributes completely. In C# this translates in a null object.</p>
  
  <p>Is there a way to change this behaviour so to have the three properties deserialized? </p>
  
  <p>Thanks in advance for any advice!</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>This is the associated namespace:</p>
  
  <pre><code>&lt;records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="structure.xsd"&gt;
      (location is within here somewhere)
  &lt;/records&gt;
  </code></pre>