title: Deserialize different element types into one object Property in C#?
body: <p>I would like to deserialize the following XML element.</p>
  
  <pre><code>&lt;error code="1" type="post"&gt;
    &lt;bad-request xmlns="blah:ns"&gt; // &lt;-- may be some different element name
      &lt;text&gt;You fail.&lt;/text&gt; // &lt;-- this is optional
    &lt;/bad-request&gt;
  &lt;/error&gt;
  </code></pre>
  
  <p>The child element <code>bad-request</code> can have several different names. I would like to deserialize that element name to an error type which is defined as enumeration in my namespace like follows.</p>
  
  <pre><code>public enum ErrorType { BadRequest = 1, Forbidden = 2, Blah = 3, ... }
  </code></pre>
  
  <p>Additionally I would like to have the <code>text</code>' element text parsed into a <code>ErrorText</code> property. So my final class would look like that.</p>
  
  <pre><code>public class Error 
  {
    public string ErrorText { get; set; }
    public ErrorType ErrorType { get; set; }
  }
  </code></pre>
  
  <p>How do I achieve something like this in C# and with deserialization?</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>My current solution seems like an overkill to me.</p>
  
  <pre><code>public class Error
  {
  
      private string _type;
  
      [XmlAttribute("type")]
      public string Type // &lt;-- there is clearly a bug in here I should not do that on the type attribute because it has nothing to do with the error type
      {
          get { return _type; }
  
          set
          {
              _type = value;
  
              switch (value)
              {
                  case "bad-request":
                      ErrorType = ErrorTypes.BadRequest;
                      ErrorText = BadRequest.Text.Value;
                      break;                    
                  default:
                      ...
                      break;
              }
          }
      }
  
      public ErrorTypes ErrorType { get; set; }
      public string ErrorText { get; set; }
  
      [XmlElement("bad-request")]
      public BadRequest BadRequest { get; set; }
  
  }
  
  public enum ErrorTypes 
  { 
      BadRequest = 0,
      Conflict = 1,
      FeatureNotImplemented = 2,
      Forbidden = 3
  }
  
  public class Text 
  {
      [XmlText]
      public string Value { get; set; }
  }
  
  public class BadRequest
  {        
      [XmlElement("text")]
      public Text Text { get; set; }
  }
  </code></pre>
  
  <p>The code above would require to have a separate class for each element name/type.</p>