c# - C# 中数字的 xml 序列化使用什么格式?

标签 c# xml-serialization

当我写的时候:

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/

相关文章:

c# - 将 IEnumerable<T> 对象序列化为字符串数组的扩展方法?

c# - 多态类型和 IXmlSerializable

c# - 防止 DataContractSerializer 中空成员的序列化

c# - 无法导入 system.web.ui.design

c# - 操作无法完成。无效指针 - Visual Studio 2015 Update 3

c# - Python for .NET : Using same . 多个版本的 NET 程序集

java - 使用 Java 将补充的 unicode 字符序列化为 XML 文档

c# - WPF MVVM 使用 BooleanToVisibilityConverter 隐藏按钮

javascript - 将 JSON 字符串/数组解析为 JS 对象

c# - Xml 反序列化附加到列表