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

     Sample Schema

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

    The child elements must appear in the order show.

    Sequence.xsd
    Copy Code
    <?xml version="1.0" encoding="utf-8" ?>
    <!--Created with Liquid Studio (https://www.liquid-technologies.com)-->
    <xs:schema elementFormDefault="qualified" 
               targetNamespace="http://www.liquid-technologies.com/sample/sequence" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="Root">
            <xs:complexType>
                <xs:sequence>
                    <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:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    All the items in the xs:sequence must appear in order they are defined and obey there Cardinality (shown explicitly on the diagram for clarity).

    Generated Code

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

    Generated Code
    Copy Code
        public partial class RootElm
        {
            public RootSeq Seq { get; set; } = new RootSeq();
    
            public partial class RootSeq
            {
                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.Seq.A = "test";
    elmWith.Seq.B = 5;
    elmWith.Seq.C = new RootElm.CElm();
    elmWith.Seq.D.Add(1);
    elmWith.Seq.D.Add(2);
    serializer.Serialize(@"..\..\Sequence_Output.xml", elmWith);  // OK
    

    Sample Output

    Sequence_Output.xml
    Copy Code
    <?xml version="1.0" encoding="utf-8"?>
    <Root xmlns="http://www.liquid-technologies.com/sample/sequence">
      <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(@"..\..\Sequence_Output.xml"); // OK
    Debug.Assert(elmWith.Seq.A == "test");
    Debug.Assert(elmWith.Seq.B == 5);
    Debug.Assert(elmWith.Seq.C != null);
    Debug.Assert(elmWith.Seq.C.Seq.Child == string.Empty);
    Debug.Assert(elmWith.Seq.D.Count == 2);
    Debug.Assert(elmWith.Seq.D[0] == 1);
    Debug.Assert(elmWith.Seq.D[1] == 2);
    

    When the valid XML file Sequence_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.

     

    See Also