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};
myChoices.ManyChoices=strChoices;
// For each item in the ManyChoices array, add an
// enumeration value.
MoreChoices[] itmChoices = new MoreChoices[]
{MoreChoices.Item,
MoreChoices.Amount,
MoreChoices.Temp};
myChoices.ChoiceArray=itmChoices;
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)
{
_items.Add(item);
_elementNames.Add(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();
posted @ Friday, July 25, 2008 1:35 PM