views:

1750

answers:

2

I'm working on a Webservice to share data between 2 ERP-systems. First ERP calls the webservice, which serializes the data-object and sends it to the second ERP.

A data object looks like this:

    <xs:complexType name="Parent">
     <xs:sequence>
      <xs:element ref="ta:ReceiptLine" maxOccurs="unbounded"/>
     </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Child">
     <xs:sequence>
      ...
      <xs:element name="SerialNo" type="xs:string" nillable="true" minOccurs="0"/>
      <xs:element name="Quantity" type="xs:int" nillable="false"/>
            ...
     </xs:sequence>
    </xs:complexType>
    ...
    <xs:element name="Child" type="ta:Child" nillable="true"/>

The classes generated by XSD:

[System.Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=false)]
public partial class Parent {
    private Child[] child;

    [System.Xml.Serialization.XmlElementAttribute("Child", IsNullable=true)]
        public Child[] Child {
            get {return this.child;}
            set {this.child = value;}
}

[System.Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://FSM4TA/DataObjects/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://FSM4TA/DataObjects/", IsNullable=true)]
    public partial class Child{
        private string serialNo;
        private int quantity;

        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string SerialNo {
            get {return this.serialNo;}
            set {this.serialNo = value;}
        }

        public int Quantity {
            get { return this.quantity;}
            set {this.quantity = value;}
        }
}

I'm serializing my data objects with XmlSerializer

The Problem Is: (On serialization) Every time in case of the Child object is empty (xsi:nil="true") XSD generates the whole Child structure anyway. And because Quantity is not nillable/nullable XSD writes 0 as value... Like this:

<Parent>
  <Child xsi:nil="true">
    <SerialNo xsi:nil="true" />
    <Quantity>0</Quantity>
  </Child>
</Parent>

I expected to get something like this:

<Parent>
  </Child xsi:nil="true">
</Parent>

The Question Is: Is there a way to prevent XSD from parsing an xsi:nil="true"-Object ??

Any suggestions?

TIA

+1  A: 

ok,

I got it now! You have to mark the Quantity property with the XmlElementAttribute explicitly!

[XmlElement(IsNullable=false)]
public int Quantity {
        get { return this.quantity;}
        set {this.quantity = value;}
    }

No idea why this hasn't been generated automatically...