float.Parse(string) xml localization issue

  • Thread starter Thread starter SharpCoderMP
  • Start date Start date
S

SharpCoderMP

i've run into some trouble using data from xml inside my app.
the scenario is simple. input data looks more or less like this:
<item>
<name>MyName</name>
<somefloat>11.5</somefloat>
</item>

nothing special as you can see. there is a part of my code that converts
<somefloat> value to float. for that i use float.Parse(string)... and i
end up with FrmatException... why? because framework is so smart that it
knows that my system locale number representation is "xxx,xx" not the
"xxx.xx" (coma instead of dot). well, great, but xml schema float
requires that "dot".

any ideas how to solve this riddle?
 
SharpCoderMP,

Call the overload that takes an IFormatProvider implementation, and then
pass the CultureInfo.InvariantCulture property. That should work.

Hope this helps.
 
Try:
float.Parse("11.5",
System.Globalization.CultureInfo.InvariantCulture.NumberFormat);

HTH

Ciaran O'Donnell
 
Use the overload that takes an IFormatProvider and pass in
System.Globalization.NumberFormatInfo.InvariantInfo. (And if you need to
handle dates use System.Globalization.DateTimeFormat.InvariantInfo)

/claes
 
Back
Top