In addition to what John mentioned on contract-first, you can at least define
the messages that go in and out of the service using XSD, and then xsd.exe to
generate the classes to work with. Doing this, you can specify in the message
that the particular dateTime element is not required with minOccurs = 0.
I did the following to demonstrate this:
Add a new .xsd file to the project.
Create a new element: MyTimeStamp
Add an element to that: CreateDateTime of dateTime type, minOccurs = 0
<xs:element name="MyTimeStamp">
<xs:complexType>
<xs:sequence>
<xs:element name="CreateDateTime" minOccurs="0" type="xs:dateTime"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Then used XSDObjectGenVSAddIn to generate the class file for that schema.
xsd.exe would work as well.
Anyways, the resulting code is this:
[XmlRoot(ElementName="MyTimeStamp",Namespace=Declar ations.SchemaVersion,IsNullable=false),Serializabl e]
public class MyTimeStamp
{
[XmlElement(ElementName="CreateDateTime",IsNullable =false,Form=XmlSchemaForm.Qualified,DataType="date Time",Namespace=Declarations.SchemaVersion)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public DateTime __CreateDateTime;
[XmlIgnore]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool __CreateDateTimeSpecified;
[XmlIgnore]
public DateTime CreateDateTime
{
get { return __CreateDateTime; }
set { __CreateDateTime = value; __CreateDateTimeSpecified = true; }
}
[XmlIgnore]
public DateTime CreateDateTimeUtc
{
get { return __CreateDateTime.ToUniversalTime(); }
set { __CreateDateTime = value.ToLocalTime(); __CreateDateTimeSpecified =
true; }
}
public MyTimeStamp()
{
__CreateDateTime = DateTime.Now;
}
}
You may be able to glean enough info to add some attributes to your datetime
field to get the desired result, however it's not apparent from the
attributes how that's achieved.
Anyways, I had a simple webmethod to return an instance of MyTimeStamp:
public MyTimeStamp Get()
{
return new MyTimeStamp();
}
note the CreateDateTime field is not specified. The resulting WSDL is
minOccurs = 0 for that field, and the sample XML response is this:
<MyTimeStamp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.org/XMLSchema.xsd" />
No CreateDateTime in there cause it's optional, which is what you want.
By defining at least the message contracts in Xml Schema you can better
predict the results.
Ron
"Zohar" wrote:
Hi,
I've created a web service which uses a structure that I've defined. This
structure contains a string and a DateTime. When the WSDL is created the
string field is assigned a minoccurs of 0, while the DateTime field is
assigned a minoccurs of 1. How can I control the multiplicity of the field?
More specifically, I'd like to state that the DateTime field is optional
(minoccurs 0).
Thanks,
Zohar.