Example Schema:
<complexType name="Dog">...</complexType>
<complexType name="Cat">...</complexType>
<complexType name="ArrayOfDog">
<sequence>
<element name="Dog" type="tns:Dog minOccurs="0" maxOccurs="unbounded" />
</sequence>
</complexType>
<complexType name="Foo">
<sequence>
<element name="Bar" type="string"/>
<element name="Baz" type="anyType"/>
</sequence>
</complexType>
Running this through .NET's wsdl.exe generates code similar to the following:
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Dog[]))]
public partial class Dog { ... }
public partial class Cat { ... }
public partial class Foo {
private string barField;
private object bazField;
}
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.
Example XML document 1:
<Foo>
<Bar>Hello</Bar>
<Baz xsi:type="Cat">
...
</Baz>
</Foo>
Example XML document 2:
<Foo>
<Bar>Hello</Bar>
<Baz xsi:type="ArrayOfDog">
<Dog>...</Dog>
<Dog>...</Dog>
</Baz>
</Foo>
Document #1 is deserialized correctly by the runtime. I get an object of type Foo with correctly deserialized fields for Bar and Baz.
Document #2 is deserialized incorrectly 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.
Has anyone come across this?
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…