posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

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
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Working with *Specified Properties.

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

Print | posted on Wednesday, July 11, 2007 8:43 PM |

Feedback

Gravatar

# re: Working with *Specified Properties.

Thanks for your post, unfortunately we have been searching for exactly this problem for nearly two days...
5/13/2008 2:52 AM | mouszeman
Gravatar

# re: Working with *Specified Properties.

But why do one have to set the specified property to "true", before using its corresponding value? It should happen on its own.
9/14/2008 5:22 PM | Aneez Kunju

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 4 and type the answer here:

Powered by: