Thursday, November 24, 2022

Lists and Arrays in XML with Liquid XML Objects

We recently received a support ticket with a question with regards to defining arrays in XML and using them within the C# code generated from Liquid XML Objects.

XML Schema (XSD)

The following demonstrates XML Schema (XSD) two ways to do this, either using an attribute list or a list of elements.

<?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="MyData">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" type="xs:string" />
                <xs:element name="UIntData" type="xs:unsignedInt" minOccurs="10" maxOccurs="10" />
            </xs:sequence>
            <xs:attribute name="ByteData" use="required">
                <xs:simpleType>
                    <xs:list itemType="xs:byte" />
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
</xs:schema>

 

XML Data

The following XML data is valid against the above XML Schema (XSD) and demonstrates a list of values in the ByteData attribute and a list of values as UIntData elements.

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio (https://www.liquid-technologies.com) -->
<MyData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:noNamespaceSchemaLocation="C:\Temp\ListExample.xsd" 
        ByteData="16 -23 -125 -114 50 -14">
    <Name>string</Name>
    <UIntData>5505</UIntData>
    <UIntData>5285</UIntData>
    <UIntData>4753</UIntData>
    <UIntData>8634</UIntData>
    <UIntData>8294</UIntData>
    <UIntData>4300</UIntData>
    <UIntData>990</UIntData>
    <UIntData>1661</UIntData>
    <UIntData>3025</UIntData>
    <UIntData>5070</UIntData>
</MyData>

 

XML Data Binding Source Code (C#)

If you use Liquid XML Objects to generate code from the XSD, you will see the following properties in the generated code to access the array data:

// attribute list will use...
public System.SByte[] ByteData { get; set; } = new System.SByte[] {};

// element list will use...
public List<System.UInt32> UIntData { get; } = new List<System.UInt32>();


What is Liquid XML Objects?

Liquid XML Objects provides a direct replacement for Microsoft Visual Studio's xsd.exe with better XSD 1.0 standard support and support for W3C XSD 1.1.

Liquid XML Objects Features

  • Fully Integrated as a Microsoft Visual Studio extension.
  • Generates an easy to use class library for C# and Visual Basic .Net.
  • Replace xsd.exe with better XSD 1.0 support and adds support for XSD 1.1.
  • Generate simple light weight classes which follow the structure of the XML.
  • Supports .Net Core, .Net Standard and .Net Framework
  • Supports W3C XML Schema for XSD 1.0 and XSD 1.1 standards.
  • Support for the most complex XML standards.
  • Royalty free distribution of compiled code and runtime.

Download a Free Trial:
https://www.liquid-technologies.com/trial-download