Regional settings in XML ?

O

ORC

How is the best way (the standard way) to handle changes in regional
settings in XML?
If a XML file is generated with the dot as the decimal symbol (like in US)
and a user changes to use a comma as the decimal symbol (like in franche).
Let me give an example: An application saves a number value defined as
double in a node. The value will be converted to a string as e.g. 3.1415
(regional settings = US). A user changes the regional settings to 'Franche'
and when the application reads the value it will return an error because it
doesn't understand the dot sign.

Thanks,
Ole
 
J

Jon Skeet [C# MVP]

ORC said:
How is the best way (the standard way) to handle changes in regional
settings in XML?
If a XML file is generated with the dot as the decimal symbol (like in US)
and a user changes to use a comma as the decimal symbol (like in franche).
Let me give an example: An application saves a number value defined as
double in a node. The value will be converted to a string as e.g. 3.1415
(regional settings = US). A user changes the regional settings to 'Franche'
and when the application reads the value it will return an error because it
doesn't understand the dot sign.

The best thing to do, IMO, is store them in the invariant culture -
separate the XML form from the form the user will see.
 
O

ORC

Thanks. Do you have a simple example of how to save and load a variable in
invariant format - I have been looking for things like that, but I can't
find a solution.

Thanks
Ole
 
J

Jon Skeet [C# MVP]

ORC said:
Thanks. Do you have a simple example of how to save and load a
variable in invariant format - I have been looking for things like
that, but I can't find a solution.

If you use the XmlConvert class, it already does things in an invariant
format. If you're using another way of saving and loading, you'll need
to explain how you're doing it so we can see whether there's a way you
can do it in a culture neutral way.
 
O

ORC

Hi,

When writing a value to the XML I use: MyUnknownTypeValue.ToString();
When reading from the XML I use:
MyUnknownTypeValue =
Convert.ChangeType(LoadedValue, TypeOf(MyUnknownTypeValue), null );

Thanks,
Ole

When saving I simply use
 
J

Jon Skeet [C# MVP]

ORC said:
When writing a value to the XML I use: MyUnknownTypeValue.ToString();
When reading from the XML I use:
MyUnknownTypeValue =
Convert.ChangeType(LoadedValue, TypeOf(MyUnknownTypeValue), null );

Right. I suggest you use the XmlConvert class instead of that then, to
give you more control.

(You *could* temporarily change the culture of the current thread, but
I think that's a bit of a grotty hack way of doing it.)
 
O

ORC

The XmlConvert doesn't unfortunately accept an object as a parameter e.g.
like this:
"XmlConvert(field.GetValue(MyClass));"
But:
"field.GetValue(MyClass).ToString();"
works!

the field is derived in a foreach fieldInfo loop.

So the temporarily change to a US culture would probably be the only
solution left, or am I missing something? But how do I change the culture of
the current thread?

Thanks
Ole
 
J

Jon Skeet [C# MVP]

ORC said:
The XmlConvert doesn't unfortunately accept an object as a parameter e.g.
like this:
"XmlConvert(field.GetValue(MyClass));"
But:
"field.GetValue(MyClass).ToString();"
works!

Well yes, you'll need to do a bit more work - it's not a one line
change. You need to call the appropriate overload of
XmlConvert.ToString when going to XML, and
XmlConvert.ToByte/Boolean/etc on the way back.
the field is derived in a foreach fieldInfo loop.
So the temporarily change to a US culture would probably be the only
solution left, or am I missing something? But how do I change the culture of
the current thread?

I really don't recommend it, but you can use
Thread.CurrentThread.CurrentCulture = ...;
 
O

ORC

I found a great workaound that works very well - the best part is that it
can be done in ONE line and that is 'Clean' code:

string elementString = Convert.ToString(Convert.ChangeType(fieldValue,
typeof(string), System.Globalization.CultureInfo.InvariantCulture));

Ole
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top