Problem:
The value of DateTime property is assigned but its not included in the serialized xml.
SomeClass msg = new SomeClass();
msg.generated_on = DateTime.Now ;
if we serialize this we will find this:
<?xml version="1.0"?>
<SomeClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
as opposed to
<?xml version="1.0"?>
<SomeClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" generated_on="12/07/2007 6:29:28 PM" />
Here is the c# code of SomeClass
public class SomeClass
{
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.String generated_on;
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool generated_onSpecified;
}
Clarification:
==========
if we create an object of SomeClass and assign a value to the
member generated_on
(example msg.generated_on = DateTime.Now)
the XmlSerializer is not 100% happy unless we also specify
true to member generated_onSpecified
(example msg.generate_onSpecified = true);
WSDL.exe
After looking at the proxy that I have with me now, which is generated by WSDL.exe, I also found for all Members that are bool and DateTime it creates an additional bool member with the suffix of "Specified" by default.
For example:
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.DateTime generated_on {}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool generated_onSpecified {}
If we look at the generated code we will find the same pattern is followed all over.
After investigating further I found:
Many of the types that are created by the Microsoft Visual Studio 2005 proxy generator include properties with names that are appended with "Specified". These properties are used along with the property of the same name that does not include the "Specified" suffix.
If the *Specified property is not set to true, the request will be sent without the property. This can result in unpredictable Web service behavior.
read more....
https://msdn2.microsoft.com/en-us/library/bb402199.aspx
and looking more further into XmlSerialization:
If a schema includes an element that is optional (minOccurs = '0'), or if the schema includes a default value, you have two options. One option is to use System.ComponentModel.DefaultValueAttribute to specify the default value, as shown in the following code. Another option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of propertyNameSpecified. For example, if there is a field named "MyFirstName" you would also create a field named "MyFirstNameSpecified" that instructs the XmlSerializer whether or not to generate the XML element named "MyFirstName".
read more.....
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
Suggestion:
Bottom line is, make sure that generate_onSpecified is set to "true" if you want the value of the generated_on to be serialized properly.
SomeClass msg = new SomeClass();
msg.generated_onSpecified = true;
msg.generated_on = DateTime.Now ;
and then serialize, ..... it should be fine.
Thankyou and hope this helps