Geeks With Blogs

News My Blog has been MOVED to
Michael Freidgeim's OLD Blog My Blog has been MOVED to
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

    /// </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();


Posted on Friday, July 25, 2008 1:35 PM | Back to top

Comments on this post: XmlChoiceCollection class to access XSD generated properties for choice XML elements

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Michael Freidgeim | Powered by: