开发者

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

开发者 https://www.devze.com 2023-02-08 17:09 出处:网络
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.

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 开发者_StackOverflow社区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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消