title: How can I deserialize xml with 2 different types of the same xml element and property name?
body: <p>In the database, we have an xml field that contains 2 validation schemas; the old one does not have a namespace, the new one does. The reason for this is that we had to version one of the properties. Here is an example of the differences:</p>
  
  <p>Version 1</p>
  
  <pre><code>&lt;PropertyA&gt;
    &lt;PropertyA1&gt;false&lt;/PropertyA1&gt;
    &lt;PropertyA2&gt;3.23&lt;/PropertyA2&gt;
  &lt;/PropertyA&gt;
  </code></pre>
  
  <p>Version 2</p>
  
  <pre><code>&lt;ts:PropertyA xmlns:ts="http://www.example.com/v2"&gt;
    &lt;ts:PropertyA1&gt;false&lt;/ts:PropertyA2&gt;
    &lt;ts:PropertyA2&gt;
      &lt;ts:PropertyA2a&gt;
          &lt;ts:PropertyA2a1&gt;City 1&lt;/ts:PropertyA2a1&gt;
          &lt;ts:PropertyA2a2&gt;3.23&lt;/ts:PropertyA2a2&gt;
      &lt;/ts:PropertyA2a&gt;
      &lt;ts:PropertyA2b&gt;
          &lt;ts:PropertyA2b1&gt;City 2&lt;/ts:PropertyA2b1&gt;
          &lt;ts:PropertyA2b2&gt;1.21&lt;/ts:PropertyA2b2&gt;
      &lt;/ts:PropertyA2b&gt;
    &lt;/ts:PropertyA2&gt;
  &lt;/ts:PropertyA&gt;
  </code></pre>
  
  <p>Basically, we just create multiple options for PropertyA2...</p>
  
  <p>So now the isue is deserialization. This object needs to be deserialized into the same data object in the app code and the problem is that the element name is the same so the serializer is obviously having trouble figuring out which object to deserialize into since sometimes the database will return Version 1 and sometimes it will return Version 2.</p>
  
  <p>Here is an example of the data class being used for serialization and my current approach that isn't quite working:</p>
  
  <pre><code>[Serializable]
  public class MyDataClass
  {
      // ... other stuff
  
      [XmlElement(Name = "PropertyA", typeof(V1.PropertyA), Namespace = "")]
      public V1.PropertyA PropertyAV1 { get ;set; }
  
      [XmlElement(Name = "PropertyA", typeof(V2.PropertyA), Namespace = "http://www.example.com/v2")]
      public V2.PropertyA PropertyAV2 { get; set; }
  }
  
  [Serializable]
  public class V1.PropertyA
  {
      public bool PropertyA1 { get; set; }
  
      public decimal PropertyA2 { get; set; }
  }
  
  [Serializable]
  public class V2.PropertyA
  {
      public bool PropertyA1 { get; set; }
  
      public List&lt;SomeOtherThing&gt; PropertyA2 { get; set; }
  }
  </code></pre>
  
  <p>When I go to deserialize V1, it works fine. When I go to deserialize V2, i get an error <code>Did not expect &lt;ts:PropertyA xmlns:ts="http://www.example.com/v2"&gt;</code> so I'm thinking there's a parameter I'm missing in the deserialize method:</p>
  
  <pre><code>public MyDataClass Deserialize(string xml)
  {
      var s = new XmlSerializer(typeof (MyDataClass));
      MyDataClass info = null;
      using (var r = new StringReader(xml))
      {
          info = (MyDataClass) s.Deserialize(r);
      }
      return info;
  }
  </code></pre>
  
  <p>I believe you can set the expected namespace in the serializer, but since I don't know what the namespace is going to be until I actually inspect the xml document, I'm not sure how to proceed. </p>
  
  <p>So my question is this: Is what I'm trying to do even possible? Am I on the right track? Is there a better solution that is maybe less contrived? How can I have the serializer deal with the new namespace and deserialize to the correct properties?</p>