XML : XSD2Code not generation optional Enum

I'm running into a rather large problem generating C# classes from several XSD's. During serialization an Enum-member get's the wrong value. The property isn't present in the XML i'm serializing, so I would expect it to be NULL or '0', but instead it gets the value '1' which is the first available value in the Enum.

Let's say I've got an XSD called 'person.xsd' and it looks like this;

person.xsd

  <?xml version="1.0" encoding="UTF-8"?>  <xs:schema>      <xs:import namespace="http://www.somewhere.nl/folder1/1_1/basisschema/schema/1" schemaLocation="basisschema.xsd"/>      <xs:complexType name="Person">          <xs:annotation>              <xs:documentation>Personal information</xs:documentation>          </xs:annotation>          <xs:sequence>              <xs:element name="SomeType" type="ns:CDT_SomeType" minOccurs="0">                  <xs:annotation>                      <xs:documentation>Some type.</xs:documentation>                  </xs:annotation>              </xs:element>          </xs:sequence>      </xs:complexType>  </xs:schema>    

The person XSD includes a base schema called 'basisschema.xsd'. That file looks like this;

basisschema.xsd

  <xs:schema targetNamespace="http://www.somewhere.nl/folder1/1_1/basisschema/schema/1" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://www.somewhere.nl/folder1/1_1/basisschema/schema/1">      <xs:complexType name="CDT_SomeType">          <xs:annotation>              <xs:documentation>Some type.</xs:documentation>          </xs:annotation>          <xs:sequence>              <xs:element name="SomeEnumElement" type="ns:LDT_SomeEnumElement">                  <xs:annotation>                      <xs:documentation>Some enum</xs:documentation>                  </xs:annotation>              </xs:element>          </xs:sequence>      </xs:complexType>  </xs:schema>    

The generated code looks like this;

Client.cs

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.81.0")]  [System.SerializableAttribute()]  [System.ComponentModel.DesignerCategoryAttribute("code")]  [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.somewhere.nl/folder1/1_1/basisschema/schema/1")]  [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.somewhere.nl/folder1/1_1/basisschema/schema/1", IsNullable = true)]  public class Client  {      private CdtSomeType _someType;        public Client()      {          _someType = new CdtSomeType();      }        public CdtSomeType SomeType      {          get          {              return _someType;          }          set          {              _someType = value;          }      }     }    

CdtSomeType.cs

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.81.0")]  [System.SerializableAttribute()]  [System.ComponentModel.DesignerCategoryAttribute("code")]  [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.somewhere.nl/folder1/1_1/basisschema/schema/1")]  public class CdtSomeType  {      private LDTSomeEnumElement _someEnumElement;        public LDTSomeEnumElement SomeEnumElement      {          get          {              return _verklaringField;          }          set          {              _verklaringField = value;          }      }  }    

I expected an optional property of the type 'CdtSomeType' in Person.cs, however this didn't happen. Now when we serialize an XML-file that matches this XSD using the following piece of code;

  using (var reader = new StringReader(message))  {      var myXmlObject = (Person)serializer.Deserialize(reader);        //This value isn't available in the XML so I want this to be NULL instead of the default Enum value.      var test = myXmlObject.SomeType.SomeEnumElement;  }    

Then the property 'SomeEnumElement' has the default enum value (1) instead of NULL or '0'. This happens during serialization. What could I possibly do to solve this issue?

No comments:

Post a Comment