Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

wsdl

wcf

I am using wsdl.exe to convert a WSDL file and Types.xsd file into a C# file. The wsdl file specifies optional variables (minOccurs="0" maxOccurs="1"), and the generated .NET type handles this by created two fields - one for the variable (e.g. status) and one to let you know if it's specified (statusSpecified).

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

Is there a different, better tool that will generate .NET types from WSDL? I am using .NET 4, so it would be useful if the generated types took advantage of features like Nullable types.

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

Regarding WCF, this article pointed me in the direction of using 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 like so: svcutil *.wsdl *.xsd /language:C#). Unfortunately, svcutil doesn't seem to do any better with using Nullable types instead of xSpecified variables.

like image 690
Pat Avatar asked Sep 27 '10 22:09

Pat


1 Answers

no, unless you modify your xsd schema. Read this article about xsd

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

private System.Nullable<bool> x;

private bool xSpecified;

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

private System.Nullable<bool> x;

Difference between nullable and Specified:

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

Hope it helps :)

like image 125
3lvinaz Avatar answered Oct 21 '22 06:10

3lvinaz