title: In .NET XML deserialization, how can I allow polymorphic use of Array types?
body: <p>Example Schema:</p>
  
  <pre><code>&lt;complexType name="Dog"&gt;...&lt;/complexType&gt;
  &lt;complexType name="Cat"&gt;...&lt;/complexType&gt;
  
  &lt;complexType name="ArrayOfDog"&gt;
      &lt;sequence&gt;
          &lt;element name="Dog" type="tns:Dog minOccurs="0" maxOccurs="unbounded" /&gt;
      &lt;/sequence&gt;
  &lt;/complexType&gt;
  
  &lt;complexType name="Foo"&gt;
      &lt;sequence&gt;
          &lt;element name="Bar" type="string"/&gt;          
          &lt;element name="Baz" type="anyType"/&gt;
      &lt;/sequence&gt;
  &lt;/complexType&gt;
  </code></pre>
  
  <p>Running this through .NET's wsdl.exe generates code similar to the following:</p>
  
  <pre><code>[System.Xml.Serialization.XmlIncludeAttribute(typeof(Dog[]))]
  
  public partial class Dog { ... }
  
  public partial class Cat { ... }
  
  public partial class Foo {
      private string barField;
      private object bazField;
  }
  </code></pre>
  
  <p>It appears that wsdl.exe is trying to be "smart" and realize that my ArrayOfDog is really just a wrapper type that can be encoded as a C# array.  This works fine when ArrayOfDog is explicitly referenced in another data type.  However, when ArrayOfDog is used polymorphically (e.g. as a substitution for xsd:anyType) this breaks.  It appears to break because the .NET runtime knows nothing about the complexType named "ArrayOfDog" - it has basically thrown away this information in favor of just using native C# arrays.</p>
  
  <p>Example XML document 1:</p>
  
  <pre><code>&lt;Foo&gt;
      &lt;Bar&gt;Hello&lt;/Bar&gt;
      &lt;Baz xsi:type="Cat"&gt;
          ...
      &lt;/Baz&gt;
  &lt;/Foo&gt;
  </code></pre>
  
  <p>Example XML document 2:</p>
  
  <pre><code>&lt;Foo&gt;
      &lt;Bar&gt;Hello&lt;/Bar&gt;
      &lt;Baz xsi:type="ArrayOfDog"&gt;
          &lt;Dog&gt;...&lt;/Dog&gt;
          &lt;Dog&gt;...&lt;/Dog&gt;
      &lt;/Baz&gt;
  &lt;/Foo&gt;
  </code></pre>
  
  <p>Document #1 is deserialized correctly by the runtime.  I get an object of type Foo with correctly deserialized fields for Bar and Baz.  </p>
  
  <p>Document #2 is deserialized <strong>incorrectly</strong> by the runtime.  I get an object of type Foo with a correctly deserialized field for Bar, but for the Baz field I get System.XML.XMLNode[].  My guess is because the runtime knows nothing about any type binding for an entity named "ArrayOfDog".  You might think that the XmlInclude directive "XmlIncludeAttribute(typeof(Dog[]))" would handle this, but it doesn't appear to be working.</p>
  
  <p>Has anyone come across this?</p>
  
  <p>Is there an elegant solution here?  The workaround I'm thinking of using is to wrap my "ArrayOf" type in another type and include that in the subsitution for the xsd:anyType.</p>