title: Ignoring root element name when deserializing XML from USPS Address Validation API
body: <p>Visual Studio 2019, C# .NET Core 3.1, no 3rd party libraries</p>
  
  <p>I am consuming the USPS Address Validation web service which sends its reply in XML. However, the service will actually send one of two different replies, depending on whether or not it encounters an error. For example, a normal reply looks like this:</p>
  
  <pre><code>&lt;AddressValidateResponse&gt;
   &lt;Address ID="0"&gt;
    &lt;Address1&gt; STE K&lt;/Address1&gt;
    &lt;Address2&gt;29851 AVENTURA&lt;/Address2&gt;
    &lt;City&gt;RANCHO SANTA MARGARITA&lt;/City&gt;
    &lt;CityAbbreviation&gt;RCHO STA MARG&lt;/CityAbbreviation&gt;
    &lt;State&gt;CA&lt;/State&gt;
    &lt;Zip5&gt;92688&lt;/Zip5&gt;
    &lt;Zip4&gt;2014&lt;/Zip4&gt;
    &lt;DeliveryPoint&gt;83&lt;/DeliveryPoint&gt;
    &lt;CarrierRoute&gt;C057&lt;/CarrierRoute&gt;
    &lt;Footnotes&gt;N&lt;/Footnotes&gt;
    &lt;DPVConfirmation&gt;Y&lt;/DPVConfirmation&gt;
    &lt;DPVCMRA&gt;N&lt;/DPVCMRA&gt;
    &lt;DPVFootnotes&gt;AABB&lt;/DPVFootnotes&gt;
    &lt;Business&gt;Y&lt;/Business&gt;
    &lt;CentralDeliveryPoint&gt;N&lt;/CentralDeliveryPoint&gt;
    &lt;Vacant&gt;N&lt;/Vacant&gt;
   &lt;/Address&gt;
  &lt;/AddressValidateResponse&gt;
  </code></pre>
  
  <p>However, if the service encounters an error, it will return the following XML:</p>
  
  <pre><code>&lt;Error&gt;
   &lt;Number&gt;12345&lt;/Number&gt;
   &lt;Source&gt;USPS.WEB.API&lt;/Source&gt;
   &lt;Description&gt;Error encountered&lt;/Description&gt;
  &lt;/Error&gt;
  </code></pre>
  
  <p>Unfortunately in both instances the http status code is 200 for a successful http request, even though the service itself generated an error.</p>
  
  <p>I'm not sure how to handle conditional deserialization of this scenario. I wrote a routine which deserializes the regular response into a C# class, however, if I receive the ERROR reply, then routine chokes with an "unexpected XML" error when it attempts to deserialize into the AddressValidateResponse class.</p>
  
  <p>Is there a way to consolidate both of these replies into a single class and tell the deserializer to, essentially, "ignore" the root element name when it deserializes. In other words, could I create a class (abbreviated for example purposes like this:</p>
  
  <pre><code>public class MyClass
  {
   public Int32 Number {get; set;} = 0;
   public String Source {get; set;} = "USPS";
  ...
   public Address Address {get; set;} = null;
  }
  </code></pre>
  
  <p>and then deserialize either the ERROR or ADDRESSVALIDATERESPONSE resultant XML into this 'umbrella' class, where I would get the error information if the service throws an error, but get the correct address information (with a default 0 error number) if the response is successful?</p>
  
  <p>I attempted this on my end, but all I get are XML deserialization errors, because (in my example) the root element name in the XML response from USPS is either ERROR or ADDRESSVALIDATERESPONSE and that doesn't match the XMLRoot element name of my custom class.</p>
  
  <p>Since there doesn't seem to be a way to allow for multiple possible root element names in deserialization, is there a way to ignore the name and have the deserializer populate the children elements?</p>
  
  <p>Or, is there a way to have an umbrella class, like:</p>
  
  <pre><code>public class MyClass
  {
   public Error Error { get; set; } = null;
   public AddressValidateResponse AddressValidateResponse { get; set; } = null 
  }
  </code></pre>
  
  <p>and then have the deserializer populate either the error or addressvalidateresponse sub-classes depending on the root element tag in the response?</p>
  
  <p>I suppose I can look at the web response string for the presence of either ERROR or ADDRESSVALIDATERESPONSE and write code to conditionally serialize into a temporary class which I then copy into the umbrella class, but that seems like a hack to me.</p>
  
  <p>Wondering what other solutions folks may think of, or if there's a way to natively have this scenario handled without writing a lot of conditional code. Unfortunately this is the way the API response, which is poor design IMO (other APIs like UPS and Fedex encapsulate error information into a single response which will contain nulls for other fields when there's an error, but for some reason USPS insists on sending one of two different replies.</p>