Deserializing "null" value into double

  • Thread starter Thread starter Michael Groeger
  • Start date Start date
M

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
 
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
 
XmlSerializer of course, not XmlDeserializer ;)

Michael Groeger said:
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
 
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
 
Back
Top