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

 Sample Schema

Consider the following schema, Root must contain the child nodes A,B,C and 0-n D's.

The child items can appear in any order but any D's must be grouped together

i.e.

In XSD 1.0 maxOccurs must be 1 for elements within an xs:all, but this limitation is lifted in XSD 1.1

all.xsd
Copy Code
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2018 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" targetNamespace="http://www.liquid-technologies.com/sample/all" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Root">
        <xs:complexType>
            <xs:all>
                <xs:element name="A" type="xs:string" />
                <xs:element name="B" type="xs:int" />
                <xs:element name="C">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Child" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="D" type="xs:int" minOccurs="0" maxOccurs="unbounded" />
            </xs:all>
        </xs:complexType>
    </xs:element>
</xs:schema>

All the items in the xs:all must obey there Cardinality (shown explicitly on the diagram for clarity).

Generated Code

The following code is generated for the schema all.xsd.

Generated Code
Copy Code
    public partial class RootElm
    {
        public RootAll All { get; set; } = new RootAll();

        public partial class RootAll
        {
            public System.String A { get; set; } = "";
            public System.Int32 B { get; set; }
            public CElm C { get; set; } = new CElm();
            public List<System.Int32> D { get; } = new List<System.Int32>();
        }

        public partial class CElm
        {
            public CSeq Seq { get; set; } = new CSeq();

            public partial class CSeq
            {
                public System.String Child { get; set; } = "";
            }
        }
    }
The Lx... attributes, qualifying namespaces and comments have been removed for clarity.
A, B and C are required (as their cardinality is 1-1), however it is possible to set A and C to null. While the object model will allow this, an error will be raised if they are serialized (see Error Handling).

Writing Sample Code 

We can use the following code to build an instance of the Root Element

Sample Code
Copy Code
LxSerializer<RootElm> serializer = new LxSerializer<RootElm>();

RootElm elmWith = new RootElm();
elmWith.All.A = "test";
elmWith.All.B = 5;
elmWith.All.C = new RootElm.CElm();
elmWith.All.D.Add(1);
elmWith.All.D.Add(2);
serializer.Serialize(@"..\..\All_Output.xml", elmWith);  // OK

Sample Output

All_Output.xml
Copy Code
<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid XML Objects (Registered to 'NA ') -->
<Root xmlns="http://www.liquid-technologies.com/sample/all">
  <A>test</A>
  <B>5</B>
  <C>
    <Child />
  </C>
  <D>1</D>
  <D>2</D>
</Root>

Reading Sample Code

The following code will read back in the files created above.

Sample Code
Copy Code
LxSerializer<RootElm> serializer = new LxSerializer<RootElm>();

RootElm elmWith = serializer.Deserialize(@"..\..\All_Output.xml"); // OK
Debug.Assert(elmWith.All.A == "test");
Debug.Assert(elmWith.All.B == 5);
Debug.Assert(elmWith.All.C != null);
Debug.Assert(elmWith.All.C.Seq.Child == string.Empty);
Debug.Assert(elmWith.All.D.Count == 2);
Debug.Assert(elmWith.All.D[0] == 1);
Debug.Assert(elmWith.All.D[1] == 2);

When the valid XML file All_Output.xml is read and produces an object model identical to the one serialized out in the code above.

The code generated from xs:all and xs:sequence if functionally the same, xs:choice has different behaviour.