none
Avoiding xsi:nil="true" in WCF client RRS feed

  • Question

  • Hi!

    I am consuming a Java based webservice.

    Within my WCF client I notice that all fields/properties marked as optional in the WSDL are serialized as:

    <ItemDetails>
    <PartNumber>1622120106</PartNumber>
    <PrimaryVendorId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <SubgroupId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <ItemGroupId>1622</ItemGroupId>
    <ItemType>0</ItemType>
    <Companyname="CGDK">
    <StockQuantity>227.00</StockQuantity>
    <MinimumQuantity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <Unit>Pcs</Unit>
    <BreakageType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <PackageSize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    <AbcCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    </Company>
    </ItemDetails>

    Please notice all the xsi:nil="true" values corresponding to the non required fields/properties. Since I am sending quite a lot of "ItemDetails" this behavior causes quite a lot of network trafic not to mention the overhead at the receivers end when parsing these null values.

    I was wondering if I somehow could avoid these null values. I would be interested in not having the fields/properties at all if possible. What are my options?

    Please notice that my client proxie is autogenerated by VS2010 from the webservice WSDL. I would very much like to be able to autoupdate the reference in the future.

    Thank You very much for any of Your feedback

    Kind regards

    Torben Philippsen

    Thursday, April 11, 2013 7:41 AM

Answers

  • Hi Chilberto and mbj79!

    Thank You very much for your assistance.

    I went with another approach having the publisher of the webservice remove all nullable properties from the wsdl.

    The problem as You both pointet out was that the original wsdl was looking something like this (snippet):

    <xsd:complexType name="ItemDetailsType" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:sequence xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="PartNumber" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossDepthMeter" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossHeightMeter" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossWidthMeter" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="NetWeightKg" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="TaraWeightKg" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="PrimaryVendorId" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="SubgroupId" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemGroupId" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemType" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Barcode" type="ns:ItemBarcodeType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Company" type="ns:ItemCompanyType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Language" type="ns:ItemlanguageType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    </xsd:sequence>
    </xsd:complexType>

    notice all the nillable="true" props.

    In the new version of the wsdl the same part looks like this:

    xsd:complexType name="ItemDetailsType" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:sequence xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="PartNumber" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossDepthMeter" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossHeightMeter" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossWidthMeter" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="NetWeightKg" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="TaraWeightKg" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="PrimaryVendorId" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="SubgroupId" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemGroupId" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemType" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Barcode" type="ns:ItemBarcodeType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Company" type="ns:ItemCompanyType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Language" type="ns:ItemlanguageType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    </xsd:sequence>
    </xsd:complexType>

    That solved the problem for me.

    Once again. Thank You very much for spendingYour time helping me out.

    Regards, Torben

    Friday, April 12, 2013 9:16 AM

All replies

  • Hello Torben,

    You can control this by modifying the generated proxy.  This might break the contract though as the java processor might require these fields to be sent (hence why the contract was generated with 

    IsNullable = true

    You can try setting these values to false to test the results. 

    Cheers,

    Jeff


    Jeff

    Thursday, April 11, 2013 10:54 PM
  • This might break the contract though as the java processor might require these fields to be sent (hence why the contract was generated with 

    IsNullable = true

    It can't break the contract if the elements are optional according to the XML Schema. Optional is not the same thing as nullable!

    To suppress null/nil elements, set EmitDefaultValue = false in the DataMember attributes of the properties in the DataContract:

    [DataMember(EmitDefaultValue = false)]
    public string SomeProperty;

    Do NOT set it for properties that have IsRequired = true!

    // Don't do this!
    [DataMember(IsRequired = true, EmitDefaultValue = false)]
    public string RequiredProperty;

    For more information, see



    Marcus Björklund

    Friday, April 12, 2013 1:10 AM
  • Hi Chilberto and mbj79!

    Thank You very much for your assistance.

    I went with another approach having the publisher of the webservice remove all nullable properties from the wsdl.

    The problem as You both pointet out was that the original wsdl was looking something like this (snippet):

    <xsd:complexType name="ItemDetailsType" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:sequence xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="PartNumber" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossDepthMeter" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossHeightMeter" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossWidthMeter" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="NetWeightKg" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="TaraWeightKg" nillable="true" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="PrimaryVendorId" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="SubgroupId" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemGroupId" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemType" nillable="true" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Barcode" type="ns:ItemBarcodeType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Company" type="ns:ItemCompanyType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Language" type="ns:ItemlanguageType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    </xsd:sequence>
    </xsd:complexType>

    notice all the nillable="true" props.

    In the new version of the wsdl the same part looks like this:

    xsd:complexType name="ItemDetailsType" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:sequence xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="PartNumber" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossDepthMeter" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossHeightMeter" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="GrossWidthMeter" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="NetWeightKg" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="TaraWeightKg" type="xsd:decimal" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="PrimaryVendorId" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="SubgroupId" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemGroupId" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element minOccurs="0" name="ItemType" type="xsd:string" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Barcode" type="ns:ItemBarcodeType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Company" type="ns:ItemCompanyType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    <xsd:element maxOccurs="unbounded" minOccurs="0" name="Language" type="ns:ItemlanguageType" xmlns:xsd="http://www.w3.org/2001/XMLSchema"/>
    </xsd:sequence>
    </xsd:complexType>

    That solved the problem for me.

    Once again. Thank You very much for spendingYour time helping me out.

    Regards, Torben

    Friday, April 12, 2013 9:16 AM