The effects of the XSD element <xs:choice> on the generated code
Overview
The XSD <choice> entity defines a container for other elements or element groups. Of the items contained within the <choice> only 1 can appear within the XML file.
Sample XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="ParentChoice">
<xs:complexType>
<xs:choice>
<xs:element name="FirstChild" type="xs:string"/>
<xs:element name="SecondChild" type="xs:string"/>
<xs:element name="ThirdChild" type="xs:string"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

The Generated Code
The code generated for the sample XSD is shown here in its UML representation, note that there are 3 string properties (FirstChild, SecondChild & ThirdChild) corresponding with the child elements within ParentSeq. There are also 3 other properties IsValidFirstChild, IsValidSecondChild & IsValidThirdChild, these indicate whether the corresponding property contains a valid value. The property ChoiceSelectedElement, indicates which of the 3 elements is selected at any given time. If one element is selected (i.e. FirstChild contains a value), and then another is given a value (say SecondChild is set to "Some Text"), then FirstChild will become invalid (IsValidFirstChild will return false, and reading from FirstChild with raise an exception), and ChoiceSelectedElement will return the string "SecondChild".

Sample Code
The Code below shows how to read in the XML document and determine which item within the choice was set.
// create an instance of the class to load the XML file into
choiceLib.ParentChoice elm = new choiceLib.ParentChoice();
elm.FromXmlFile("c:\\Choice.xml");
// we can find out child child element is selected using ChoiceSelectedElement
if (elm.ChoiceSelectedElement == "SecondChild")
{
Trace.Write("The second child element was present and has the value ", elm.SecondChild);
}
// or by looking at the IsValid flags
Debug.Assert(elm.IsValidFirstChild == false);
Debug.Assert(elm.IsValidSecondChild == true);
Debug.Assert(elm.IsValidThirdChild == false);
The Choice.xml file used in the sample above
<?xml version="1.0"?> <!--Created by Liquid XML Data Binding Libraries (www.liquid-technologies.com) for simon--> <ParentChoice>
<SecondChild>Some Data 2</SecondChild>
</ParentChoice>
Notes
When reading an XML document, if more than one child element is contained within the parent element <ParentChoice>, then an exception will be raised.
When writing, if you have already set on of the properties on the ParentChoice object, then setting a different one, will unset the previously set one and set the one you just assigned a value to.