Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What format is used for xml-serialization of numbers in C#?

When I write:

var d = 12.34D;
d.ToString();

it gives "12,34", but when I serialize object with double field, it gives "12.34"

It is because XmlSerializer uses some specific format/culture? What exactly? I've looked into Double's source code but not seen implementation of IXmlSerializable. Thanks.

like image 664
Feofilakt Avatar asked Jun 26 '18 08:06

Feofilakt


People also ask

Is XML a serialization format?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

What is the correct way of using XML serialization?

As with the CreatePo method, you must first construct an XmlSerializer, passing the type of class to be deserialized to the constructor. Also, a FileStream is required to read the XML document. To deserialize the objects, call the Deserialize method with the FileStream as an argument.

What format formats does .NET serialization support?

NET ships with two formatters: a binary formatter and a Simple Object Access Protocol (SOAP) formatter. The binary formatter generates a compact binary representation of the object's state. It's relatively fast to serialize an object into binary format and to deserialize an object from binary format.


1 Answers

The XmlSerializerWriter uses XmlConvert.ToString to convert the values.

The relevant part from that class is this:

return value.ToString("R", NumberFormatInfo.InvariantInfo);

So it uses the invariant culture, which happens to output the string that is conforming to the XML RFC (So a period as decimal separator).

Format specifier "R" is documented here:

The round-trip ("R") format specifier attempts to ensure that a numeric value that is converted to a string is parsed back into the same numeric value. This format is supported only for the Single, Double, and BigInteger types.

It means that the other end will produce the same double result when deserializing the string value.

like image 122
Patrick Hofman Avatar answered Nov 11 '22 14:11

Patrick Hofman