I am serialiizing a c# object to an XML file to save some application
settings. The idea is to ship this xml file along with the application
and deserialize after the application is installalled to use it as a
default settings file. Works fine on machines having the same culture
(en-US) settings. But goes wrong when the object is serialized on a
machine with the culture set to english and and deserialized on
another machine with another culture (it-IT Italy). For this case I
can see that the problem is that in Italy the decimal seperator is a
comma (,) instead of a dot(.) so deserializing ints and doubles
creates a mess.
So my question is, what is the correct way of handling xml
serialization and globalization?
Any help is most appreciated.
Nishant
Are you doing any of the serialization yourself? Perhaps via
IXmlSerializable?
Marc
Joachim
This is what it looks like:
____________________
XSD:
____________________
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestClass" targetNamespace="http://tempuri.org/
TestClass.xsd" elementFormDefault="qualified" xmlns="http://
tempuri.org/TestClass.xsd" xmlns:mstns="http://tempuri.org/
TestClass.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TestClass">
<xs:complexType>
<xs:sequence>
<xs:element name="Amount" type="xs:int">
</xs:element>
<xs:element name="Language" type="xs:language" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
____________________
class:
____________________
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd",
"2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,
Namespace="http://tempuri.org/TestClass.xsd")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://
tempuri.org/TestClass.xsd", IsNullable=false)]
public partial class TestClass {
private int amountField;
private string languageField;
/// <remarks/>
public int Amount {
get {
return this.amountField;
}
set {
this.amountField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(DataType="language")]
public string Language {
get {
return this.languageField;
}
set {
this.languageField = value;
}
}
}
____________________
Unit test:
____________________
[Test]
public void SerializationLocalozation()
{
TestClass tc = new TestClass();
tc.Amount = 1000;
StreamWriter writerEnUS = new
StreamWriter("SerializationTest_en_US.xml");
StreamWriter writerNlNL = new
StreamWriter("SerializationTest_nl_NL.xml");
Thread.CurrentThread.CurrentCulture =
CultureInfo.GetCultureInfo("en-US");
tc.Language =
Thread.CurrentThread.CurrentCulture.ToString();
XmlSerializer xmls = new XmlSerializer(typeof(TestClass));
xmls.Serialize(writerEnUS, tc);
writerEnUS.Close();
Thread.CurrentThread.CurrentCulture =
CultureInfo.GetCultureInfo("nl-NL");
tc.Language =
Thread.CurrentThread.CurrentCulture.ToString();
xmls.Serialize(writerNlNL, tc);
writerNlNL.Close();
}
____________________
Resulting xml en-US:
____________________
<?xml version="1.0" encoding="utf-8"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/
TestClass.xsd">
<Amount>1000</Amount>
<Language>en-US</Language>
</TestClass>
____________________
Resulting xml nl-NL:
____________________
<?xml version="1.0" encoding="utf-8"?>
<TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/
TestClass.xsd">
<Amount>1000</Amount>
<Language>nl-NL</Language>
</TestClass>