Liquid XML Data Binder 2019
xs:union
Liquid XML Objects (C#, Visual Basic .Net) > Using the Generated Code > XML Schema Handling > xs:union

Consider the BoolInt element in the schema.

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="MyElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="BoolInt" minOccurs="0" maxOccurs="unbounded">
                    <xs:simpleType>
                        <xs:union memberTypes="xs:boolean xs:int" />
                    </xs:simpleType>
                </xs:element>
                <xs:element name="DateGDay" minOccurs="0" maxOccurs="unbounded">
                    <xs:simpleType>
                        <xs:union memberTypes="xs:date xs:gDay" />
                    </xs:simpleType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio (https://www.liquid-technologies.com) -->
<MyElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:noNamespaceSchemaLocation="UnionSample.xsd">
    <SimpleUnion>10</SimpleUnion>
    <SimpleUnion>-5</SimpleUnion>
    <SimpleUnion>true</SimpleUnion>
    <SimpleUnion>false</SimpleUnion>
    <DateUnion>---15</DateUnion>
    <DateUnion>2018-01-18</DateUnion>
</MyElement>

 BoolInt

The generator will create a following class to represent the BoolInt type.

Class Generate for the BoolInt type
Copy Code
    public class BoolIntUnion : Union<System.Boolean, System.Int32>
    {
        [LxUnion(LxValueType.Value, XsdType.XsdBoolean)]
        public BoolIntUnion(System.Boolean value) : base(value) {}
        [LxUnion(LxValueType.Value, XsdType.XsdInt)]
        public BoolIntUnion(System.Int32 value) : base(value) {}
    }

This union class may hold a System.Boolean or a System.Int32, and can be used as follows.

Ns.MyElementElm elm = new Ns.MyElementElm();

elm.Seq.BoolInt.Add(new Ns.BoolIntUnion(true));
elm.Seq.BoolInt.Add(new Ns.BoolIntUnion(5));


// The value can be determined by examining the Value property
if (elm.Seq.BoolInt[0].Value is int)
{
    int i = (int)elm.Seq.BoolInt[0].Value;
    Console.WriteLine($"Int value {i}");
}
else if (elm.Seq.BoolInt[0].Value is bool)
{
    // Another way to access the value is via the typed property
    bool b = elm.Seq.BoolInt[0].Type1;
    Console.WriteLine($"Bool value {b}");
}

DateGDay

The DateGDay element may contain a xs:date or xs:gDay, both of which are represented in the C# using the class LxDateTime. So the following class is generated to hold the values.

Class Generate for the BoolInt type
Copy Code
    public class DateGDayUnion : Union<LiquidTechnologies.XmlObjects.LxDateTime>
    {
        [LxUnion(LxValueType.Value, XsdType.XsdDate)]
        [LxUnion(LxValueType.Value, XsdType.XsdGDay)]
        public DateGDayUnion(LiquidTechnologies.XmlObjects.LxDateTime value) : base(value) {}
    }

This union class may only hold a  LxDateTime, so technically is not required, but it keeps the generated code consistent.

Ns.MyElementElm elm = new Ns.MyElementElm();

//elm.Seq.DateGDay.Add(new Ns.DateGDayUnion(true));
elm.Seq.DateGDay.Add(new Ns.DateGDayUnion(LxDateTime.CreateDate(2018, 5, 18)));
elm.Seq.DateGDay.Add(new Ns.DateGDayUnion(LxDateTime.CreateGDay(4)));

// The union can only contain LxDateTime based values, so no need for casting
Debug.Assert(elm.Seq.DateGDay[0].Value is LxDateTime);
LxDateTime dt = elm.Seq.DateGDay[0].Type1;
Console.WriteLine($"DateTime value {dt}");

// Note: other types of date time can be added to the union, but they will be reported
// as validation errors when the XML is serialized.
elm.Seq.DateGDay.Add(new Ns.DateGDayUnion(LxDateTime.CreateGYear(2018)));

If a date/time value other than the allowable ones (xs:date, xs:gDay) are read then a validation error will occur, and although it is possible to enter a LxDateTime of a different type into the model, a validation error will be raised when the class is serialized.