Liquid XML Objects (C#, Visual Basic .Net) / Using the Generated Code / XML Schema Handling / xs:union
In This Topic
    In This Topic

    Consider the BoolInt element in the schema.

    <?xml version="1.0" encoding="utf-8" ?>
    <!--Created with Liquid Studio (>
    <xs:schema elementFormDefault="qualified" xmlns:xs="">
        <xs:element name="MyElement">
                    <xs:element name="BoolInt" minOccurs="0" maxOccurs="unbounded">
                            <xs:union memberTypes="xs:boolean xs:int" />
                    <xs:element name="DateGDay" minOccurs="0" maxOccurs="unbounded">
                            <xs:union memberTypes="xs:date xs:gDay" />
    <?xml version="1.0" encoding="utf-8"?>
    <!-- Created with Liquid Studio ( -->
    <MyElement xmlns:xsi="" 


    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}");


    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.