0

I'm loading floats from an XML document.

For example from the following line:

  <Attack name="Sword Slash" damage="10.0" stat="Strength" multiplier="2.0" />

If I read this and then parse it to a float, the damage is 100 instead of 10.0, and the multiplier is 20 instead of 2.0 aswell. I think this is because of problems with CultureInfo (not sure on this).

I tried fixing it by changing the line that reads the data from:

this.damage = float.Parse(reader.Value);

To:

this.damage = float.Parse(reader.Value.ToString(CultureInfo.CreateSpecificCulture("en-US")));

However, this does not seem to fix anything.

Any solution?

anthonytimmers
  • 258
  • 1
  • 8

1 Answers1

1

Try this instead:

this.damage = float.Parse(reader.Value, CultureInfo.InvariantCulture);

However, it would probably make things easier if you used XML serialization instead of manually parsing the XML.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Thanks, that works. Will this ensure it will work on any PC? (different languages etc.) – anthonytimmers Sep 15 '14 at 23:27
  • @anthonytimmers, yes, the invariant culture is always the same (as the name implies). It's associated with the English language, but not with a specific country. See MSDN: http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.invariantculture.aspx – Thomas Levesque Sep 15 '14 at 23:28