When an XSD is translated into a code model, properties are either mapped as optional, mandatory or a collection.
This would be straightforward but some of the types used are classes and some are structures (which can not be set to null), so when a struct can be null its wrapped with the Nullable<T> class.
The short hand for Nullable<T> is T?
So Nullable<int> which can be written int?
Any XSD element or compositor (xs:all/xs:choice/xs:sequence) with a maxOccurs value greater than 1 (or 'unbounded') is mapped as a collection.
Collections are always represented as a generic List<T> which is automatically created and only accessible via a getter
Code Generated for a Collection Property |
Copy Code
|
---|---|
public List<System.Int32> D { get; } = new List<System.Int32>(); |
Any XSD element or compositor with minOccurs=1 and maxOccurs=1 is mapped as a mandatory property (see note 1).
When the value in a mandatory property is represented as a struct, then its not possible for it to be missing, but when its represented as a class then its possible for it to be set to null. This is will not raise an error in the object model, but will raise an error when it is serialized.
Code Generated for a Mandatory Property |
Copy Code
|
---|---|
public System.Int32 A { get; set; } public System.String B { get; set; } = String.Empty; public System.Byte[] C { get; set; } = byte[0]; public Generated.ChildElm D { get; set; } = new Generated.ChildElm(); |
Any XSD element or compositor with minOccurs=0 and maxOccurs=1 is mapped as an optional property (see note 1).
When the property type is represented as a struct its wrapped with a Nullable<T> class, when Nullable<T>.HasValue is false it is considered to be missing.
When the property type is represented by an object, then it is considered missing when it is null.
Code Generated for a Mandatory Property |
Copy Code
|
---|---|
public System.Int32? A { get; set; } public System.String B { get; set; } = null; public System.Byte[] C { get; set; } = null; public Generated.ChildElm D { get; set; } = null; |
Note 1: Other factors may cause a property that looks like it should be mandatory to be represented as an optional property in the generated code. For example if the property is mandatory and contained within a xs:choice then it will be made optional.