|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Deserializing "null" value into doubleHi,
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 If you can use coalesce(<double field name>,0.0) as <double field name> it
may cure it. -- Show quoteBrad "Software is like melted pudding..." "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 > > > Or initialize the value in the class to 0.00- maybe in New()...
-- Show quoteBrad "Software is like melted pudding..." "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 > > > 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 > 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 > > > > 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 |
|||||||||||||||||||||||