all groups > dotnet xml > february 2005 >
You're in the

dotnet xml

group:

XMLWriter and NumberDecimalSeparator



Re: XMLWriter and NumberDecimalSeparator Derek Harmon
2/21/2005 2:37:39 PM
dotnet xml: [quoted text, click to view]

XmlTextWriter produces data representations that are formatted
using the invariant culture.

The XML Schema Datatypes W3C recommendation, which an
ADO.NET DataSet depends on to represent strongly-typed data
in XML, specifies in Section 3.2.3.1 that the lexical representation
of a decimal datatype must use the period as it's decimal separator.

http://www.w3.org/TR/xmlschema-2/#decimal

Section 3.2.5.1 indicates that the lexical representation of a double
datatype must represent it's mantissa and exponent as decimals,

http://www.w3.org/TR/xmlschema-2/#double

The lexical representations for these datatypes are consistent with
ISO 11404 which covers how numeric data (for instance) should
be represented to prevent ambiguities introduced by the data when
it is going to be consumed within different cultural contexts.

[quoted text, click to view]

Here is why that's a bad idea. Suppose you're serializing a decimal
value of 2.501 into XML using a different decimal separator. If
you serialize in a culture where the decimal separator is "," and the
thousands separator is "." then this lexical representation actually
represents the number two thousand five hundred one.

However, when that value gets read later in a culture where the
decimal separator is "." and the thousands separator is "," it gets
interpretted as two and five hundred one thousandths.

I hope that code isn't responsible for air traffic control. :-)

Heathrow - Charlie 9-9-9 we're turning you over to Laguardia
air traffic control. You're showing an altitude of 2,501 meters.
Over.

Laguardia - Flight Charlie 9-9-9, this is Laguardia flight tower in
New York. We see that your altitude is 2 1/2 feet. Is something
wrong? Over.

If you're going to be displaying this to the end user (as opposed to
performing further processing on it as a double or decimal datatype)
then this formatted lexical representation should be typed as a string.

That is, you should make the column a string and format it before
serializing to XML.


Derek Harmon

XMLWriter and NumberDecimalSeparator Vyacheslav Lanovets
2/21/2005 4:02:58 PM
Hello, All!

Is it possible to change parameters of locale for writing XML file
(with XMLTextWriter)?

I want DataSet.WriteXML() to use NumberDecimalSeparator "." instead of ","

Regards, Vyacheslav

Re: XMLWriter and NumberDecimalSeparator Martin Honnen
2/21/2005 5:45:54 PM


[quoted text, click to view]


[quoted text, click to view]

I don't think the result of WriteXml depends on the locale (or culture
as .NET calls it), for instance the output in the following example is
always the same, even if the culture is changed:

DataSet dataSet = new DataSet("DoubleTest");
DataTable dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("double", typeof(double)));
DataRow dataRow = dataTable.NewRow();
dataRow["double"] = 2.5;
dataTable.Rows.Add(dataRow);
dataSet.Tables.Add(dataTable);
dataSet.WriteXml(Console.Out);
Console.WriteLine();

string[] cultures = new string[] { "en-US", "en-GB", "de-DE", "de-CH"};
foreach (string cultureName in cultures) {
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
Console.WriteLine("Culture is {0}. XML is: ",
Thread.CurrentThread.CurrentCulture);
dataSet.WriteXml(Console.Out);
Console.WriteLine();
}

The decimal separator there is always '.' as it should be for double data.

Perhaps I misunderstand what you try to do, please explain in more
detail if you still have problems.


--

Martin Honnen
Re: XMLWriter and NumberDecimalSeparator Vyacheslav Lanovets
2/22/2005 11:24:29 AM
Hello, Martin!
You wrote on Mon, 21 Feb 2005 17:45:54 +0100:

MH> I don't think the result of WriteXml depends on the locale (or culture
MH> as .NET calls it), for instance the output in the following example is
MH> always the same, even if the culture is changed:

Sorry, I made a mistake and the problem was in my code:

object[] ins_row = ....;
cnv_ds.Tables[0].Rows.Add(ins_row);

Rows.Add() performs automatic conversion from "double" to "String" because
DataType of the column is String instead of double. And by default
the conversion uses current Culture.

Regards, Vyacheslav

AddThis Social Bookmark Button