XmlChoiceCollection class to access XSD generated properties for choice XML elements

When I used XML Schema Definition Tool (Xsd.exe) ,  it creates proxy class with extra ItemsChoiceType array and enum for xsd:choice  elements. 

MSDN XmlChoiceIdentifierAttribute Class article shows example of XSD, generated proxy class and how to fill  pairs of arrays:

// Populate an object array with three items, one

      // of each enumeration type. Set the array to the

      // ManyChoices field.

      object[] strChoices = new object[]{“Food”,  5, 98.6};


      // For each item in the ManyChoices array, add an

      // enumeration value.

      MoreChoices[] itmChoices = new MoreChoices[]





I found that support two arrays for items itself and for EmlementNames is error prone and created helper generic XmlChoiceCollection class, that allows to add items and types inone Add method.


#region  Namespace Imports

using System;

using System.Collections.Generic;

using System.Text;

#endregion  //Namespace Imports


    /// <summary>

    /// see http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlchoiceidentifierattribute(printer).aspx

    /// </summary>

    public class XmlChoiceCollection<TItem,TItemsChoiceEnum>


        public List<TItem> _items;

        public List<TItemsChoiceEnum> _elementNames;

        public XmlChoiceCollection()


            _items=new List<TItem>() ;

            _elementNames=new List<TItemsChoiceEnum>();


        public void Add(TItem item, TItemsChoiceEnum elementName)





        public TItem[] ItemsToArray()


           return   _items.ToArray();


        public TItemsChoiceEnum[] ElementNamesToArray()


            return _elementNames.ToArray();




Example of use:
XmlChoiceCollection<MyElement, ItemsChoiceType> choiceCollection = new XmlChoiceCollection<MyElement, ItemsChoiceType>();
for (int i = 0; i < Count; i++)
   choiceCollection .Add(new MyElement(), ItemsChoiceType.type1);

request.Items = choiceCollection.ItemsToArray();
request.ItemsElementName = choiceCollection.ElementNamesToArray();