How to define the culture that the XmlSerializer uses

后端 未结 2 493
半阙折子戏
半阙折子戏 2021-01-20 23:32

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

// Create object by deserializing the given xml document
var serializer = new XmlSerializer         


        
相关标签:
2条回答
  • 2021-01-21 00:02

    XmlSerializer class use the format configuration of your OS, it is configured in region section, in order your program uses a specific format, you could assign directly to the Thread, somenthing like below. For example if you live in a country of Latinoamerica is pretty common to use , insted of . that it is the standar representation.

    //Write this code at the begining of the program 
    CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
    culture.NumberFormat.NumberDecimalSeparator = "."; //Force use . insted of ,
    System.Threading.Thread.CurrentThread.CurrentCulture = culture;
    
    0 讨论(0)
  • 2021-01-21 00:04

    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.

    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).

    Evidence in MSDN docs

    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.

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

    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.

    on Conversion of XML Data Types.

    0 讨论(0)
提交回复
热议问题