Google 网上论坛不再支持新的 Usenet 帖子或订阅项。历史内容仍可供查看。

XML Serialization and Internationalization

已查看 9 次
跳至第一个未读帖子

Nishant Mehta

未读,
2008年5月16日 01:11:562008/5/16
收件人
Hi all,

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

Marc Gravell

未读,
2008年5月16日 03:26:102008/5/16
收件人
How are you doing the serialization? My understanding is that
XmlSerializer will use invariant formatting for common types...

Are you doing any of the serialization yourself? Perhaps via
IXmlSerializable?

Marc

Joachim Van den Bogaert

未读,
2008年5月16日 04:28:192008/5/16
收件人
Could you please give an example of your xml? I tried it out with an
xsd, an xml and a class.
The result showed me that integers were stored without formatting
(which was expected).

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>

0 个新帖子