I have a serializable class with a decimal property called Amount. My current culture is say hu-HU (decimal seperator is comma ","). I serialize my object to an xml. What I get is an Xml with the Amount value with a period (".") separator.
Here is the object.
[Serializable]
public class
Deposit
{
private decimal _amount = 0;public
decimal Amount
{
get { return _amount; }
set { _amount =
value; }
}
}
following is the code to test serialization..
Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("hu-HU");Deposit
d = new Deposit();
d.Amount = 123.763467543m;
XmlSerializer s =
new XmlSerializer(typeof(Deposit));
StringWriter w =
new StringWriter(Thread.CurrentThread.CurrentCulture);XmlWriter
xw = XmlWriter.Create(w);
Anyone encountered this? Is it a common behaviour that serializer will not consider the current culture? I dont want to go nuts writing the custom serialization for this reason
Well XML serialization uses a standardized number and date/dateTime format, the standard is the W3C schema datatype specification
http://www.w3.org/TR/xmlschema-2/.
So don't expect XmlSerializer to pay attention to the thread's CultureInfo, it intentionally uses a standardized format to ensure you can serialize/deserialize independent of the culture/locale.
If you want to output numbers in your local format then XmlSerializer is not the right tool for you.
Martin Honnen --- MVP Data Platform Development
My blog
Sajid1105
Member
1 Points
3 Posts
XML Serialization and NumberFormatting
Dec 30, 2008 02:42 AM|LINK
I have a serializable class with a decimal property called Amount. My current culture is say hu-HU (decimal seperator is comma ","). I serialize my object to an xml. What I get is an Xml with the Amount value with a period (".") separator.
Here is the object.
[Serializable] public class Deposit{
private decimal _amount = 0;public decimal Amount{
get { return _amount; } set { _amount = value; }}
}
following is the code to test serialization..
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("hu-HU");Deposit d = new Deposit();d.Amount = 123.763467543m;
XmlSerializer s = new XmlSerializer(typeof(Deposit)); StringWriter w = new StringWriter(Thread.CurrentThread.CurrentCulture);XmlWriter xw = XmlWriter.Create(w);s.Serialize(xw, d);
xw.Close();
string serializedText = s.ToString();following is the value of serializedText
<?xml version=\"1.0\" encoding=\"utf-16\"?><Deposit xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Amount>123.763467543</Amount></Deposit>
I might be missing something silly.
Thanks in advance.
manojlev
Participant
1503 Points
283 Posts
Re: XML Serialization and NumberFormatting
Dec 30, 2008 06:21 AM|LINK
Hi,
Actually you miss
<?xml version=\"1.0\" encoding=\"utf-16\"?><Deposit xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<Deposit> --------------------------------------------> Deposit node you miss
<Amount>123.763467543</Amount>
</Deposit>
See it will work now
Thank You,
Manoj Chavan
-----------------------------------------
Manoj Chavan
SE(C#, ASP.NET, C++)
Want Your Own Website?
Mauli softwares
If this post helps you then plz Mark It as Answer
Sajid1105
Member
1 Points
3 Posts
Re: XML Serialization and NumberFormatting
Dec 30, 2008 06:41 AM|LINK
deposit node is there, which is the root node.
but there is a typo..
string serializedText = s.ToString();
should bestring serializedText = w.ToString();
Anyone encountered this? Is it a common behaviour that serializer will not consider the current culture? I dont want to go nuts writing the custom serialization for this reason
Martin_Honnen
Star
14481 Points
2006 Posts
MVP
Re: XML Serialization and NumberFormatting
Dec 30, 2008 11:01 AM|LINK
Well XML serialization uses a standardized number and date/dateTime format, the standard is the W3C schema datatype specification http://www.w3.org/TR/xmlschema-2/.
So don't expect XmlSerializer to pay attention to the thread's CultureInfo, it intentionally uses a standardized format to ensure you can serialize/deserialize independent of the culture/locale.
If you want to output numbers in your local format then XmlSerializer is not the right tool for you.
My blog
Sajid1105
Member
1 Points
3 Posts
Re: XML Serialization and NumberFormatting
Dec 31, 2008 05:30 AM|LINK
Thanks Martin. I checked it.
I will be formatting the data complying with W3C standard first, before I re-format it to whatever culture I want.
Thanks again for the input.
Sajid