以文化不敏感的方式序列化/反序列化数据的正确方法是什么?
我需要将数据保存到 xml,然后以一种文化中序列化的数据可以在另一种文化中进行解析的方式读回。
在我看来,有两种方法可以实现这一目标。我可以确保所有解析/字符串/等。方法正在使用不变区域性,或者我可以将线程的区域性设置为不变,执行所有(反)序列化,然后将其切换回之前的状态。
我知道第一种方法被认为是正确的方法,但是切换线程的区域性似乎更容易并且更不容易出现错误。为什么首选第一种方法有理由吗?还有其他我可能不知道的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
的
类,以及WriteValue
方法>XmlWriterXmlReader
类。您还可以使用
XmlConvert
类。所有这些方法都会生成与文化无关的 XML 标准格式。
Use the
WriteValue
methods of theXmlWriter
class, and theReadContentAs
methods of theXmlReader
class.You can also use the methods of the
XmlConvert
class.All of these methods produce XML-standard formats, which are culture-independent.