How to create Nullable fields / variables from WSDL instead of additional fields / variables

I use wsdl.exe to convert the WSDL file and the Types.xsd file to a C # file. The wsdl file indicates optional variables ( minOccurs="0" maxOccurs="1" ), and the generated .NET type handles this by creating two fields: one for the variable (for example, status ) and one to let you know if it is specified ( statusSpecified )

Is there a way to use the wsdl tool only to create a single field that is Nullable (i.e. if not null, it is specified)? (If this helps, I think I can modify the wsdl file to have nillable="true" elements.)

Is there any other, better tool that will generate .NET types from WSDL? I am using .NET 4, so it would be useful if the generated types used functions of type Nullable.

NOTE. I just realized that I am using the wsdl tool from .NET 2, and that newer projects should use WCF for this. Any pointers to the way WCF get what I want?

As for WCF, in this article I pointed to the use of the svcutil tool (which was already in my PATH, so I could just run it from the command line in the folder with the wsdl and xsd files as follows: svcutil *.wsdl *.xsd /language:C# ). Unfortunately, svcutil does not seem to do anything better using Nullable types instead of xSpecified variables.

+6
wsdl wcf
source share
1 answer

not unless you change your xsd schema. Read this article about XSD

If you have an element with minOccurs="0" and nillable="true" it will still generate an xSpecified field.

 private System.Nullable<bool> x; private bool xSpecified; 

If you want the field to be nullable, then the element in xsd needs minOccurs="1" and nillable="true" .

 private System.Nullable<bool> x; 

Difference between nullable and specified:

  • if the field is NULL, then the field will be serialized even if it is NULL: <minzero xsi:nil="true"><minzero>
  • if the specified field is false. then the field will not be serialized.

Hope it helps :)

0
source share

All Articles