I've generated some C# classes from some 3GPP XSDs (multiple XSD files/namespaces) and it works great for serialization apart from one problem with concrete instances of an abstract type used in a substitution group.
Firstly, the relevant parts of the schema:
(genericNrm.xsd)
<element name="ManagedElement">
<complexType>
<complexContent>
<extension base="xn:NrmClass">
<sequence>
...
<choice minOccurs="0" maxOccurs="unbounded">
<element ref="xn:IRPAgent"/>
<element ref="xn:ManagedElementOptionallyContainedNrmClass"/>
<element ref="xn:VsDataContainer"/>
</choice>
</sequence>
</extension>
</complexContent>
</complexType>
</element>
<element
name="ManagedElementOptionallyContainedNrmClass"
type="xn:NrmClass"
abstract="true"
/>
(eutran.xsd)
<element name="ENBFunction" substitutionGroup="xn:ManagedElementOptionallyContainedNrmClass">
<complexType>
<complexContent>
<extension base="xn:NrmClass">
<sequence>
<element name="attributes" minOccurs="0">
<complexType>
<all>
<element name="userLabel" type="string" minOccurs="0"/>
... etc
The XML serialised from a simple ManagedElement with contained ENBFunction is:
<ManagedElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="1234" xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.625#genericNrm">
<ManagedElementOptionallyContainedNrmClass xmlns:q1="http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm" xsi:type="q1:ENBFunction" id="1234">
<q1:attributes>
<q1:userLabel>label</q1:userLabel>
</q1:attributes>
</ManagedElementOptionallyContainedNrmClass>
</ManagedElement>
The built in visual studio XML validation complains about the element, stating "This is an invalid xsi:type 'http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm:ENBFunction'.
So is the serialized XML wrong or the validation? Is it something to do with the separate namespaces?
I can deserialize the XML just fine but I need the generated XML to be schema compliant (without changing the schemas). I found that if I manually change the XML to the following, the error disappears (and I find it easier to read also):
<ManagedElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="1234" xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.625#genericNrm">
<q1:ENBFunction xmlns:q1="http://www.3gpp.org/ftp/specs/archive/32_series/32.765#eutranNrm" id="1234">
<q1:attributes>
<q1:userLabel>label</q1:userLabel>
</q1:attributes>
</q1:ENBFunction>
</ManagedElement>
Can I force the serializer to output it this way?
Thanks for looking...
I solved this by manually editing the code generated from the XSD. An XmlElementAttribute is needed on the ManagedElement classes Items collection to ensure the serialisation works correctly:
This attribute is required for all classes inherited from ManagedElement to ensure the correct one is used at serialization time.