当我写的时候:
var d = 12.34D;
d.ToString();
它给出 "12,34"
,但是当我用双字段序列化对象时,它给出 "12.34"
这是因为 XmlSerializer
使用了一些特定的格式/文化?究竟是什么?我查看了 Double 的源代码,但没有看到 IXmlSerializable
的实现。
谢谢。
最佳答案
XmlSerializerWriter
使用 XmlConvert.ToString
来转换值。
该类的相关部分是 this :
return value.ToString("R", NumberFormatInfo.InvariantInfo);
所以它使用不变文化,它恰好输出符合 XML RFC 的字符串(所以一个句点作为小数点分隔符)。
格式说明符 “R”
已记录 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.
这意味着另一端在反序列化字符串值时会产生相同的double
结果。
关于c# - C# 中数字的 xml 序列化使用什么格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51038772/