title: XmlSerializer: keep unknown elements
body: <p>I have a class that is serialized into/deserialized from XML and stored in/restored from a file:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Customer
  {
      public string FirstName;
      public string LastName;
  
      public Customer()
      {
      }
  
      public Customer(string firstName, string lastName)
      {
          FirstName = firstName;
          LastName = lastName;
      }
  
      public static Customer Load(TextReader reader)
      {
          XmlSerializer deserializer = new XmlSerializer(typeof(Customer));
          return (Customer)deserializer.Deserialize(reader);
      }
  
      public void Save(TextWriter writer)
      {
          XmlSerializer serializer = new XmlSerializer(GetType());
          serializer.Serialize(writer, this);
      }
  }
  </code></pre>
  
  <p>In a newer version of this class I added a new property</p>
  
  <pre class="lang-cs prettyprint-override"><code>public string MiddleName;
  </code></pre>
  
  <p>It is a common use case that a user has installed both the old and new version of my program. They both read and write the same serialized file. When the new version writes the file, all three properties (<code>FirstName</code>, <code>LastName</code>, <code>MiddleName</code>) are written. The old program reads the file but omits the unknown element <code>MiddleName</code>. It saves the file without <code>MiddleName</code>, so it's value is lost for the newer program.</p>
  
  <p>Is there a way to store the original XML when deserializing and 'merge' the unknown elements back in when serializing? The old program would ignore unknown elements but write them back into the file so they are not lost for the new program.</p>