tempy
tempy

Reputation: 1557

What is the right approach for serializing/deserializing data in a culture-insensitive manner?

I need to persist data to xml and then read it back in a way that data serialized in one culture will then be parse-able in another.

As I see it there are two ways to accomplish this. I can either make sure that all parse/tostring/etc. methods are using the invariant culture, or I could set the thread's culture to invariant, do all my (de)serializing, and then switch it back to whatever it was before.

I know that the first approach is deemed to be the right one, but switching the thread's culture seems to be easier and less bug-prone. Is there a reason for why the first approach is preferred? Is there some other approach that I am perhaps not aware of?

Upvotes: 2

Views: 337

Answers (1)

John Saunders
John Saunders

Reputation: 161783

Use the WriteValue methods of the XmlWriter class, and the ReadContentAs methods of the XmlReader class.

You can also use the methods of the XmlConvert class.

All of these methods produce XML-standard formats, which are culture-independent.

Upvotes: 2

Related Questions