Type inconsistency exception, XmlSerializer & XmlChoiceIdentifierAttribute with .NETCF2 SP1

Hi,

I've got a weird exception on such a line during runtime:

XmlSerializer xs = new XmlSerializer(typeof(IPAddress));

Exception message:

Type of choice identifier 'ItemElementName' is inconsisitent with type of 'Item'. Please use array of <namespace>.ItemChoiceType.

Here is the IPAddress class, which was generated by xsd.exe from the following schema portion:

<xsd:complexType name="IPAddress">

<xsd:choice>

<xsd:element name="ipv4Address">

<xsd:simpleType>

<xsd:restriction base="xsd:hexBinary">

<xsd:length value="4"/>

</xsd:restriction>

</xsd:simpleType>

</xsd:element>

<xsd:element name="ipv6Address">

<xsd:simpleType>

<xsd:restriction base="xsd:hexBinary">

<xsd:length value="16"/>

</xsd:restriction>

</xsd:simpleType>

</xsd:element>

</xsd:choice>

</xsd:complexType>


Answer this question

Type inconsistency exception, XmlSerializer & XmlChoiceIdentifierAttribute with .NETCF2 SP1

  • Gary Ho

    There is a workaround for this issue that works in NETCF 2.0 SP1 (and possibly earlier versions).

    1. Change the IPAddress.itemField type from byte[] to object.
    2. Change the IPAddress.Item property type from byte[] to object and rename the property from Item to itemObject.
    3. Create a new Item property defined as follows:

    [XmlIgnore]
    public byte[] Item {
    get {
    return (byte[])itemObject;
    }
    set {
    itemObject = value;
    }
    }

    The trick here is that we need to hide the fact that a byte array is being serialized from NetCF.

    This workaround should continue to work in later versions of NetCF, even after the original bug is fixed.



  • Suman Ghosh

    Bonnie,

    Thank you for your report. Using your code sample I was able to reproduce the problem and will report back here when I know more.



  • Scorpion1118

    Bonnie,

    I have confirmed that this is a bug in NetCF's XmlSerializer and I am working on a fix and looking for possible workarounds for the meantime.



  • fode

    Andrew,

    Thank you very much for the answer.

    I'll try this workaround and see afterwards if there'll be other problems popping up. Sorry for not trusting the XmlSerializer for NetCF....

    Bonnie

  • Anthony Christianson

    The same bug seems to exist in .NET 1.1 (in my case I had a collection type that could have 2 different names depending on the message it was in, same error etc). The above work around worked for my problem as well (although the types were different, masking the collection as a simple object in all but the serialization instructions (attributes) stopped the errors.

    Thanks, Gary



  • Type inconsistency exception, XmlSerializer & XmlChoiceIdentifierAttribute with .NETCF2 SP1