WCF Contract first based on xsd data types

984 views Asked by At

We are creating data types as XSD definitions. These xsd files are then imported into a WebSphere Application Server.

We want the WebSphere Application Server to be able to call a WCF web service using these datatypes.

The original xsd was as follows:

    <xs:simpleType name="Stillingsprosent">
         <xs:restriction base="xs:double"/>
    </xs:simpleType>

    <xs:element name="gjennomsnittStillingsprosent" type="Stillingsprosent" minOccurs="0" maxOccurs="1"/>

Running this through xsd.exe produces the following C# code:

public partial class GjennomsnittStillingsprosent {

private double gjennomsnittField;

private bool gjennomsnittFieldSpecified;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public double gjennomsnitt {
    get {
        return this.gjennomsnittField;
    }
    set {
        this.gjennomsnittField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool gjennomsnittSpecified {
    get {
        return this.gjennomsnittFieldSpecified;
    }
    set {
        this.gjennomsnittFieldSpecified = value;
    }
}
}

When we then use this datatype i a WCF contract it produces the following xsd:

<xs:complexType name="GjennomsnittStillingsprosent">
 <xs:sequence>
  <xs:element name="gjennomsnittField" type="xs:double"/>
  <xs:element name="gjennomsnittFieldSpecified" type="xs:boolean"/>
 </xs:sequence>
</xs:complexType>
<xs:element name="GjennomsnittStillingsprosent" nillable="true" type="tns:GjennomsnittStillingsprosent"/>
  • We would like the data contract to be the same as the original xsd.
  • We would also like not to need to edit the files after generation. (the data model is large)

The problem is related to optional fields that have minoccurs=0, these are really nullable, but xsd.exe was created before .net had nullable types.

How can we ensure that the datatypes used in the WCF contract are exactly the same as those specified in the XSD?

1

There are 1 answers

2
Chris On

Declare it something like this:

[DataContract]
public class Stillingsprosent{
[DataMember]
public double stillingsprosent;
}

Then to use it somewhere:

[DataMember(EmitDefault=false)]
public Stillingsprosent? gjennomsnittStillingsprosent;

EmitDefault=false means that it will not be emitted when its the default (ie null here)