Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlChoiceIdentifier and List issue when serializing to XML

I used Xsd2Code to generate C# classes from an XSD schema.

In the schema I have the following excerpt :

<hcparty>
  <firstname>some value</firstname>
  <familyname>some other value</familyname>
</hcparty>

The tool produced the following class:

[Serializable]
public class hcpartyType
{

    private List<string> itemsField;

    private List<ItemsChoiceType> itemsElementNameField;

    /// <summary>
    /// hcpartyType class constructor
    /// </summary>
    public hcpartyType()
    {
        itemsElementNameField = new List<ItemsChoiceType>();
        itemsField = new List<string>();
    }

    //[XmlArrayItem(typeof(ItemChoiceType))]
    [XmlChoiceIdentifier("ItemsElementName")]
    public List<string> Items
    {
        get
        {
            return itemsField;
        }
        set
        {
            itemsField = value;
        }
    }

    [XmlIgnore()]
    public List<ItemsChoiceType> ItemsElementName
    {
        get
        {
            return itemsElementNameField;
        }
        set
        {
            itemsElementNameField = value;
        }
    }      
}

public enum ItemsChoiceType
{

    /// <remarks/>
    familyname,

    /// <remarks/>
    firstname,

    /// <remarks/>
    name,
}

First I had to add [Serializable] class decoration because it was missing.

When serializing to XML I get this error :

Type of choice identifier 'ItemsElementName' is inconsistent with type of 'Items'. Please use array of System.Collections.Generic.List`1[[MyNamespace.ItemsChoiceType, ...]].

OK, so I added:

[XmlArrayItem(typeof(ItemChoiceType))]

in the code above where I commented it. I guess it is at right place. The error remains.

I read the link below, so am wondering whether the bug still applies and I have to change my List to Array.

Anyone with same design issue ? Blog post about somewhat my case

XmlSerializer issue with XmlChoiceIdentifier

like image 812
barbara.post Avatar asked Dec 01 '25 14:12

barbara.post


1 Answers

The Xml Serializer expects the type for the XmlChoiceIdentifier member to be an Array. Lists are not supported.

Try the following:

[Serializable]
public class hcpartyType
{

    private List<string> itemsField;

    private List<ItemsChoiceType> itemsElementNameField;

    [XmlChoiceIdentifier("ItemsElementName")]
    public string[] Items
    {
        get
        {
            return itemsField;
        }
        set
        {
            itemsField = value;
        }
    }

    [XmlIgnore()]
    public ItemsChoiceType[] ItemsElementName
    {
        get
        {
            return itemsElementNameField;
        }
        set
        {
            itemsElementNameField = value;
        }
    }      
}

public enum ItemsChoiceType
{

    /// <remarks/>
    familyname,

    /// <remarks/>
    firstname,

    /// <remarks/>
    name,
}
like image 72
Murph0 Avatar answered Dec 04 '25 04:12

Murph0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!