I have consumed my prepared XSD with the XML Binding Wizard.
<?xml version="1.0" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="examples" type="examples"/>
<xs:complexType name="examples">
<xs:sequence>
<xs:element name="example" type="example" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="example">
<xs:sequence>
<xs:element name="doublevalue" type="xs:double"/>
<xs:element name="decimalvalue" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
First of all, why are decimal and double treated differently by default?
XML simple type Double gets translated to Delphi native type Double and XML simple type Decimal gets translated to Delphi native type UnicodeString by default.
I have the same issue with both data types: locale conflicts
I'm German. That means the DecimalSeparator is a ,
and the ThousandSeparator is a .
by (Windows) default.
When I read my example XML as follows, then the 0.08 double becomes an 8 integer.
XML
<?xml version="1.0" encoding="UTF-8"?>
<examples>
<example>
<doublevalue>0.08</doublevalue>
<decimalvalue>1001.015</decimalvalue>
</example>
</examples>
Code
var
xmldoc: IXMLDocument;
examples: IXMLExamples;
i: Integer;
d: Double;
begin
xmldoc := TXMLDocument.Create(nil) as IXMLDocument;
try
xmldoc.LoadFromFile('C:\temp\example.xml');
examples := Getexamples(xmldoc); // Getexamples() is part of the unit generated by the Binding Wizard
for i := 0 to examples.Count - 1 do
d := examples[i].Doublevalue;
finally
examples := nil;
xmldoc := nil;
end;
end;
Right now I change the XML data type Double to a Delphi native type UnicodeString and work with a method like this:
function XMLStringToDouble(const str: string): double;
var
fs: TFormatSettings;
begin
fs := FormatSettings;
fs.DecimalSeparator := '.';
fs.ThousandSeparator := #0;
result := StrToFloat(str, fs);
end;
There is another issue when creating an XML
Code
var
xmldoc: TXMLDocument;
examples: IXMLExamples;
example: IXMLExample;
begin
xmldoc := TXMLDocument.Create(nil);
try
xmldoc.DOMVendor := MSXML_DOM;
xmldoc.Options := [doNodeAutoCreate, doNodeAutoIndent, doAttrNull, doAutoPrefix, doNamespaceDecl];
xmldoc.Active := true;
xmldoc.Version := '1.0';
xmldoc.Encoding := 'UTF-8';
examples := xmldoc.GetDocBinding('examples', TXMLExamples, '') as IXMLExamples;
example := examples.Add;
example.Doublevalue := 0.08;
example.Decimalvalue := '1001.015';
xmldoc.SaveToFile('C:\temp\example.xml');
finally
xmldoc.Free
end;
end;
I end up getting an XML with a ,
as DecimalSeparator.
<?xml version="1.0" encoding="UTF-8"?>
<examples>
<example>
<doublevalue>0,08</doublevalue>
<decimalvalue>1001.015</decimalvalue>
</example>
</examples>
1. Is there a way to treat Double in a simpler/proper fashion?
2. Can I somehow pass a TFormatSettings to XMLDocument or solve it in a completely different way?
3. How do you do it?