Home All Groups Group Topic Archive Search About

Deserializing "null" value into double

Author
25 Apr 2006 2:45 PM
Michael Groeger
Hi,

we use serialization and deserialization for import/export functionality.
When deserialising into a property of type double, we would like to be able
to deserialise from a "null" or empty string e.g. <myDouble></myDouble>. But
unfortunately a System.FormatException is thrown.

Any ideas?

Regards,
Michae

Author
25 Apr 2006 2:52 PM
Brad Roberts
If you can use coalesce(<double field name>,0.0) as <double field name> it
may cure it.
--
Brad

"Software is like melted pudding..."


Show quote
"Michael Groeger" wrote:

> Hi,
>
> we use serialization and deserialization for import/export functionality.
> When deserialising into a property of type double, we would like to be able
> to deserialise from a "null" or empty string e.g. <myDouble></myDouble>. But
> unfortunately a System.FormatException is thrown.
>
> Any ideas?
>
> Regards,
> Michae
>
>
>
Author
25 Apr 2006 2:54 PM
Brad Roberts
Or initialize the value in the class to 0.00- maybe in New()...


--
Brad

"Software is like melted pudding..."


Show quote
"Michael Groeger" wrote:

> Hi,
>
> we use serialization and deserialization for import/export functionality.
> When deserialising into a property of type double, we would like to be able
> to deserialise from a "null" or empty string e.g. <myDouble></myDouble>. But
> unfortunately a System.FormatException is thrown.
>
> Any ideas?
>
> Regards,
> Michae
>
>
>
Author
25 Apr 2006 10:38 PM
jkf35
The Double.TryParse method is perfect for this case.

- John Fullmer
Author
26 Apr 2006 7:14 AM
Michael Groeger
I am using XmlDeserializer to deserialize the XML. The Deserializer tries to
convert the value of the XML Element to double and then calls the
set-property of the class. When converting null (or an empty string) into a
double value, the XmlConvert.ToDouble() method throws the
System.FormatException. I don't see where I can call TryParse to change the
behaviour of the XmlDeserializer...

Michael

Show quote
"jkf35" <jk***@hotmail.com> schrieb im Newsbeitrag
news:1146004709.518446.244150@y43g2000cwc.googlegroups.com...
> The Double.TryParse method is perfect for this case.
>
> - John Fullmer
>
Author
26 Apr 2006 7:24 AM
Michael Groeger
XmlSerializer of course, not XmlDeserializer ;)

Show quote
"Michael Groeger" <google.n***@web.de> schrieb im Newsbeitrag
news:#OuMHEQaGHA.1220@TK2MSFTNGP02.phx.gbl...
> I am using XmlDeserializer to deserialize the XML. The Deserializer tries
to
> convert the value of the XML Element to double and then calls the
> set-property of the class. When converting null (or an empty string) into
a
> double value, the XmlConvert.ToDouble() method throws the
> System.FormatException. I don't see where I can call TryParse to change
the
> behaviour of the XmlDeserializer...
>
> Michael
>
> "jkf35" <jk***@hotmail.com> schrieb im Newsbeitrag
> news:1146004709.518446.244150@y43g2000cwc.googlegroups.com...
> > The Double.TryParse method is perfect for this case.
> >
> > - John Fullmer
> >
>
>
Author
26 Apr 2006 5:42 PM
jkf35
The way to do this would be to deserialize it into a string, then do
the TryParse on the string.


System.Xml.Serialization.XmlSerializer xmlConvert = new
System.Xml.Serialization.XmlSerializer(...);

string sVal = xmlConvert.Deserialize(...).ToString();
double dVal;

if (Double.TryParse(sVal, System.Globalization.NumberStyles.Any, null,
out dVal))
{
    //do whatever you want with dVal
}


I this example dVal will contain the parsed double if it worked,
otherwise TryParse returns false.
Obviously you want to change the Globalization, etc to whatever works
for the situation.

- John Fullmer

AddThis Social Bookmark Button