如何定义XmlSerializer使用的区域性 [英] How to define the culture that the XmlSerializer uses

查看:69
本文介绍了如何定义XmlSerializer使用的区域性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码反序列化xml配置文件:

I am deserializing a xml-configuration file using the following code:

// Create object by deserializing the given xml document
var serializer = new XmlSerializer(typeof(ConfigurationFile));
var stream = File.Open(path, FileMode.Open, FileAccess.Read);
var configFile = serializer.Deserialize(stream);

在配置中,我定义了一些浮点数,如下所示:

In the configuration I have some floating numbers defined as following:

DailyThreshold ="41.9"

DailyThreshold="41.9"

作为小数点分隔符."是否依赖于文化?我想知道如何定义序列化程序用来解析这些数字的文化吗?

As the decimal separator "." is culture dependent I am wondering how I can define the culture the serializer uses to parse these numbers?

推荐答案

如果同时使用XmlSerializer进行序列化和反序列化,则没有理由担心区域性:序列化器不关心区域性,也不会改变区域性.以任何方式序列化数据.

If you use XmlSerializer for both serializing and deserializing you have no reason to worry about culture: the serializer doesn't care about culture, neither it changes the serialized data in any way.

数据以相同的格式进行串行化,这与任何区域性设置(UI区域性,线程区域性或任何类型的区域性)均无关.

The data is alwasy serialized with the same format, which has nothing to do with any culture settings (nor UI culture, nor thread culture, nor any kind of culture).

我写这篇文章的时候并没有寻找证据,仅仅是因为XML是一种用于在不同系统和平台之间交换数据的格式,所以序列化格式和反序列化解析在所有系统中都必须相等.

I wrote this without looking for evidences simply because XML is a format for exchanging data between different systems and platforms, so the serialization format and deserialization parse must be equal in all the systems.

但是,如果您深入研究MSDN文档,则可以阅读以下内容:

However, if you dive into MSDN docs, you can read this:

XmlConvert类中的大多数方法用于在字符串和强类型格式之间转换数据.方法是与语言环境无关的.这意味着它们在进行转换时不会考虑任何语言环境设置.

The majority of the methods found in an XmlConvert class are used to convert data between strings and strongly-typed formats. Methods are locale independent. This means that they do not take into account any locale settings when doing conversion.

XML数据类型的转换.

这篇关于如何定义XmlSerializer使用的区域性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆