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

 Sample Schema

Consider the following schema, Root must contain the child elements A, B, C and another element (which can be anything).

Copy Code
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2018 (>
<xs:schema elementFormDefault="qualified" xmlns:xs="">
    <xs:element name="Root">
                <xs:element name="A" type="xs:string" />
                <xs:element name="B" type="xs:int" />
                <xs:element name="C" type="xs:date" />
                <xs:any namespace="##any" processContents="lax" />

Generated Code

The following code is generated for the any.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 LxDateTime C { get; set; }
            public XElement AnyElement { get; set; }
The Lx... attributes, qualifying namespaces and comments have been removed for clarity.

The xs:any is represented by the property AnyElement which is of type System.Xml.Linq.XElement. In this example our xs:any can contain any element in any namespace, but it is possible to limit the namespace (in XSD 1.1 you can also limit the name). If limitations are placed on the value of the xs:any then they are validated on serialization and deserialization, they are not validated when the property AnyElement is set (e.g. AnyElement can contain any element even if its not valid against the schema).

If xs:any had a maxOccurs greater than 1 then this would be a collection of System.Xml.Linq.XElement's.

You may notice that the xs:any in the schema is required, but the AnyElement property generated from is does not set a default value. This is the default behaviour, but the generation setting 'xs:any default name' allows a default element to be created for it. i.e.

With 'xs:any default name' set to 'MyDefaultElementName@MyNamespace'
Copy Code
 public XElement AnyElement { get; set; } = new XElement(((XNamespace)"MyNamespace") + "MyDefaultElementName");


Writing Sample Code 

We can use the following code to build instances of the Root Element

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

RootElm root = new RootElm();
root.Seq.A = "ValueA";
root.Seq.B = 4;
root.Seq.C = LxDateTime.Today;
root.Seq.AnyElement = new XElement("Other", new XText("My Value"));
serializer.Serialize(@"..\..\Any_Output.xml", root);

Sample Output

Copy Code
<?xml version="1.0" encoding="utf-8"?>
  <Other>My Value</Other>

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 root = serializer.Deserialize(@"..\..\Any_Output.xml"); // OK
Debug.Assert(root.Seq.A == "ValueA");
Debug.Assert(root.Seq.B == 4);
Debug.Assert(root.Seq.C == LxDateTime.Today);
Debug.Assert(root.Seq.AnyElement != null);
Debug.Assert(root.Seq.AnyElement.Name.LocalName == "Other");
Debug.Assert(root.Seq.AnyElement.Name.NamespaceName == "");
Debug.Assert(root.Seq.AnyElement.Value == "My Value");

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


See Also

Generated Code

Visual Studio Extension