Nullable types

Mar 2, 2010 at 7:30 AM
Edited Mar 2, 2010 at 8:39 AM

Hi

Xsd2Code generates for nullable attributes the PropertyXSpecified property, very nice, but it isn't marked with the attribute XmlIgnore and get serialized along with the other properties.

An good improvement would be take full advantage of nullable types and generate properties with the following pattern:

 

[XmlIgnore]
public DateTime? MyDate { get; set; }


[XmlAttribute(AttributeName="MyDate")]
public DateTime MyDateValue
{
  get { return this.MyDate.GetValueOrDefault(); }
  set { this.MyDate = value; }
}

[XmlIgnore]
public bool MyDateValueSpecified
{
  get { return this.MyDate.HasValue; }
} 

 

Whenever you want to serialize nullable types as attributes, if you don't care, you can also let serialize them as elements:

 

public DateTime? MyDate { get; set; }

 

If null serialize to: <MyDate xsi:nil="true" />

Or if you don't want a tag when is null, you can add the "specified" property:

 

public DateTime? MyDate { get; set; }
[XmlIgnore]
public bool MyDateSpecified { get { returm this.MyDate.HasValue; } }

 

 



 

Coordinator
Mar 8, 2010 at 8:00 PM

It is a very good idea.
I'm ok to integrate this development into the next release.

Thanks Pascal.

Mar 9, 2010 at 7:33 AM

Hi it was a first idea, I've improved it a little bit:

 

private DateTime? myDateField = null;

public DateTime MyDate { get { return this.myDateField.GetValueOrDefault(); } set { this.myDateField = value; } } [XmlIgnore] public bool MyDateSpecified { get { return this.myDateField.HasValue; } set { if (!value) this.MyDateField = null; } }

Not yet tested ... but it should work.

 

Feb 17, 2011 at 3:10 PM
pcabanel wrote:

It is a very good idea.
I'm ok to integrate this development into the next release.

Thanks Pascal.

Uhm, is this in the trunk yet? I can't an option to generate nullable primitives, e.g. DateTime?

Coordinator
Feb 21, 2011 at 7:37 PM

Hi igitur,

It is not in current version because i don't have time to work on the projet. (Too much work for my compagny !)
I'm going to try to work on this new feature in april.
Pascal.

Feb 22, 2011 at 7:30 AM

Thanks. Will you please update this thread when you get around to it. Good luck with your company work :) I know the feeling.

Feb 22, 2011 at 11:44 PM

I'm somewhat surprised to see this discussion regarding nullable types; I had hoped when I stumbled upon this tool that it had already stitched this whole problem up.  I mean that's what it says on the home page.

What I am discovering as I evaluate all the XSD-Code generating options is that none of them seem to elegantly handle optional attributes.  Elements are generally handled elegantly by using Nullable primitives in the code and generating empty XML elements with the xsi:nil="true" attribute.  But optional attributes don't get the same treatment; instead they get this stupid ___Specified field.

Why is that?

Forgive my ignorance, but why can't optional attributes be treated like this ...

[XmlAttribute(AttributeName="MyDate")]
public DateTime? MyDate { get; set; }

 

(Or I guess the equivalent but with change events etc.)


Feb 23, 2011 at 1:46 AM

Because we already use another tool I opted to fix that tool rather than switch to this tool.  Especially since this tool doesn't solve the problem I was having anyway.

You can see my patch here ... http://wscfblue.codeplex.com/SourceControl/list/patches  (download the patch and view the screenshot to see the effect of my little change)

 

Coordinator
Feb 23, 2011 at 7:33 PM

MrGlover,

Did you try GeneratePropertyNameSpecified=false option in xsd2code ?

thus, you don't need to set propertyNameSpecified = true

Pascal.

Feb 23, 2011 at 8:26 PM
I did not try that - to be honest I must have overlooked it or not twigged to what it was for.

Now that I am trying it again I'm getting an error :(

Cannot remove the specified item because it was not found in the specified collection.

SubType: Unspecified

Rule:
Coordinator
Feb 24, 2011 at 10:31 AM

Thank you for your feedback.

Will you share your xsd file with me please. It will help me to find  the problem.

You can send me it if you agree at xsd2code@gmail.com.

Thank you for your help.

Pascal.

Apr 29, 2011 at 12:48 PM

Hi,

I stumbled upon the same problem setting the 'specified' properties for optional elements. A setter routine for optional element which sets the additional specified property would be more than nice. The 'GeneratePropertyNameSpecified=false' option ist not helpful because it results in empty elements in the XML. This behavior leads to errors at schema validation if an optional and now  empty element has required children.

Regards Ppancho