I wonder if someone can help with the following.
I’m looking into an issue that we have discovered in our codebase. We have an model in C# that’s been generated from an xsd and within the resultant model we have enums. However, the xml that we have to deserialize (generated by a third party) doesn’t contain value for all properties, e.g. nulls are allowed in places.
For the properties that are enums, a null value in the xml is deserialized into the first value in the enum. I’m sure that with JSON libraries there would be a way to set null handling with serialisation options, but I’ve not yet found a way to handle this with .net xml serialisation types.
Is anyone aware of a way to this, so that I don’t have to manually edit the model generated from the xsd and so that the behaviour can be applied to all enum handling? I can’t see any options in the types or methods to force this kind of behaviour.
I’m looking into changing the model presently, as that may be acceptable as it’s a versioned model, so changes come with a versioning mechanism, meaning there shouldn’t be a need to re-generate the model again (allowing for manual edits). Though doing this for each model, for each version, for each enum property isn’t the preferred approach.
Example below (note, I’ve omitted irrelevant parts of the model).
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:iso:std:iso:20022:tech:xsd:head.001.001.02")]
public partial class PostalAddress24
{
public AddressType3Choice? AdrTp {
get {
return this.adrTpField;
}
set {
this.adrTpField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:iso:std:iso:20022:tech:xsd:head.001.001.02")]
public partial class AddressType3Choice {
private object itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Cd", typeof(AddressType2Code))]
[System.Xml.Serialization.XmlElementAttribute("Prtry", typeof(GenericIdentification30))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
[System.SerializableAttribute()] [System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:iso:std:iso:20022:tech:xsd:head.001.001.02")]
public enum AddressType2Code {
/// <remarks/>
ADDR,
/// <remarks/>
PBOX,
/// <remarks/>
HOME,
/// <remarks/>
BIZZ,
/// <remarks/>
MLTO,
/// <remarks/>
DLVY,
}
The xml comes with a postal address as follows, so the country (ctry) is present, but the AdrTp isn’t supplied.
<PstlAdr>
<Ctry>GB</Ctry>
</PstlAdr>
The deserialisation of the xml to the model is presently done using the standard XmlSerializer class.
If anyone has solved this please do let me know as it'll short cut further investigations.
0
First of all, XML distinguish null (xsi:nil) from value not specified (lack of node in xml). In your case it’s value not specified.
To know if node was present in the xml use *Specified
properties, for e.g.:
[XmlIgnore]
public bool AdrTpSpecified { get; set; }
XmlSerializer
sets *Specified
properties to true when it reads xml and finds related node. When it writes xml it uses *Specified
properties to determine if it should emit related node.
1