Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009


I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes www.pageflakes.com.
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.


Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET

Shahed Khan blog

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

Posted on Wednesday, July 11, 2007 8:43 PM | Back to top


Comments on this post: Working with *Specified Properties.

# re: Working with *Specified Properties.
Requesting Gravatar...
Thanks for your post, unfortunately we have been searching for exactly this problem for nearly two days...
Left by mouszeman on May 13, 2008 2:52 AM

# re: Working with *Specified Properties.
Requesting Gravatar...
But why do one have to set the specified property to "true", before using its corresponding value? It should happen on its own.
Left by Aneez Kunju on Sep 14, 2008 5:22 PM

# re: Working with *Specified Properties.
Requesting Gravatar...
I was wondering the logic behind the *Specified, and the results of your investigation (https://msdn2.microsoft.com/en-us/library/bb402199.aspx) helped! Thanks for sharing.
Left by HHV on Feb 27, 2009 1:45 AM

Your comment:
 (will show your gravatar)


Copyright © Shahed Khan | Powered by: GeeksWithBlogs.net