4

Below is a DataContract in my WCF service and its respective xsd schema shown in the wsdl file.

[Serializable]
[XmlRoot(Namespace = "http://www.example.com/l0257/services/mgnt/datatypes/0/1",IsNullable = false)]
public partial class InstrumentData
{
    private string _serialNo;
    private string _model;
    private int _pointsRecorded;

    [XmlElement(ElementName = "SerialNo", IsNullable = false)]
    public string SerialNo
    {
        get { return _serialNo; }
        set { _serialNo = value; }
    }

    [XmlElement(ElementName = "Model", IsNullable = false)]
    public string Model
    {
        get { return _model; }
        set { _model = value; }
    }

    [XmlElement(ElementName = "PointsRecorded", IsNullable = false)]
    public int PointsRecorded
    {
        get { return _pointsRecorded; }
        set { _pointsRecorded = value; }
    }
}

WSDl file contains the below info for the respective datacontract:

 <xs:complexType name="InstrumentData">
 <xs:sequence>
  <xs:element minOccurs="0" maxOccurs="1" name="SerialNo" type="xs:string" /> 
  <xs:element minOccurs="0" maxOccurs="1" name="Model" type="xs:string" /> 
  <xs:element minOccurs="1" maxOccurs="1" name="PointsRecorded" type="xs:int" /> 
  </xs:sequence>
  </xs:complexType>

Can someone please let me know what am I missing in my data-contract to get minOccurs=1 and maxOccurs=1 for the "Model" & "SerialNo" properties of Instrumentdata class.

Kishore Borra
  • 269
  • 3
  • 15
  • 1
    You can reverse engineer this by using xsd.exe command(http://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.71%29.aspx) and see how xsd tool will generate the class for you based on the xml schema – Amir Sep 26 '11 at 06:57
  • Thanks for the reply. I have amended the xsd and created a c# class file from it. But interestingly if I generate xsd file for this class file again i am getting minOccurs=0 – Kishore Borra Sep 26 '11 at 07:30

1 Answers1

4

See here for a full description of the way minOccurs is determined. It seems that for a reference type you must specify IsNullable=true in order to produce minOccurs=1.

Phil Degenhardt
  • 7,215
  • 3
  • 35
  • 46