开发者

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

开发者 https://www.devze.com 2023-01-18 00:46 出处:网络
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

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 t开发者_如何转开发his 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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消