3

Is decimal separator ('.' or ',' ) depends of CurrentCulture?

I have a problem in serialization XML. When I type ',' as separator, I have an exception. (Culture is setted as DE-de)

Regards

example ( TestProperties is my own class for testing)

TestProperties properties = new TestProperties 

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);

double tempValue = 1.23 // Or 1,23
properties.DoubleValue = tempValue;

XmlSerializer serializer = new XmlSerializer(typeof(TestProperties));
TextWriter textWriter = new StreamWriter(XMLPath);
serializer.Serialize(textWriter, properties);
textWriter.Close();


public class TestProperties
    {   
        private double _doubleValue;
        [XmlElement("Double")]
        public double DoubleValue
        {
            get { return _doubleValue; }
            set { _doubleValue = value; }
        }
    }
GrzesiekO
  • 1,179
  • 4
  • 21
  • 34

2 Answers2

4

Decimal separator is determined by the current culture, however, for XML Serialization, the current culture is not taken into account. XML convention will have to be used; decimal separator in XML will always be a point.

If you're building the XML by hand, you should use the XMLConvert class to make sure that all data-types are formatted correctly in the XML.

Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • Thanks for your replies. I have another question. I'm using XMLSerializator. Is it using InvariantCulture, or it using CurrentCulture? If current, can I use Invariant? I'm using systems with different cultures, so I fave troubles with valid serialize or deserialize beetwen these systems. – GrzesiekO Sep 09 '11 at 11:09
3

It depends entirely on the context. You mention xml; within xml, the format is usually represented in a non-cultural culture (which means: . is decimal and , is thousands etc). Similarly, xml has specific representations for dates/times.

If you are building your xml via XmlWriter, XElement, XmlSerializer (etc) this will be automatic; if you are building it by hand it could well be confused (a mix of different representations).

If the data is not in the expected format, you might have to load it into a string property (rather than, say, a float etc), and process it separately.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks for your replies. I have another question. I'm using XMLSerializator. Is it using InvariantCulture, or it using CurrentCulture? If current, can I use Invariant? I'm using systems with different cultures, so I fave troubles with valid serialize or deserialize beetwen these systems. – GrzesiekO Sep 09 '11 at 11:05
  • @ogrod87 it is *xml* - so invariant *plus* a few other things (i.e. the DateTime format is different etc); culture should not be an issue *at the point of (de)serialization* - but of course, whatever you do after that is subject to culture. Is there a specific issue you are seeing? – Marc Gravell Sep 09 '11 at 11:45
  • Yes. It happens when I'm trying serialize values with point or comma as decimal separator. Depends on culture, on XML i have different values. I want to normalize xml. No matter if I type value with comma or point, I want get in XML value with point. – GrzesiekO Sep 09 '11 at 11:50
  • @ogrod87 do you have an example of how you are trying to serialize this? otherwise I'm only guessing – Marc Gravell Sep 09 '11 at 11:57
  • 1
    @ogrod87 and with that example, what is the *desired* xml, and what is the *actual* (undesired) xml? – Marc Gravell Sep 09 '11 at 12:56
  • desire: always with dot actual: depends of culture(sometimes dot, sometimes comma, sometimes 0) – GrzesiekO Sep 09 '11 at 13:02
  • @ogrod that sounds odd... Unexpected – Marc Gravell Sep 09 '11 at 14:39
  • yeah, I know. Maybe if i will not find a solution, I will create my own xml serializer. Of course based on .net xml serializer. – GrzesiekO Sep 09 '11 at 17:18