从序列化 XML 中的小数中删除小数点
我将 XmlSerializer
与使用 xsd.exe
从 xsd
创建的类一起使用。几个月以来一直运行良好。
现在我收到报告,在某些情况下,创建的 xml 文件的所有小数都序列化,没有小数点,例如 123.45 序列化为 12345。
我无法重现该问题,但我怀疑它可能与到本地化。
这是我的第一个 C# 项目,所以我可能会忽略一些基本的东西。
本地化会导致这个问题吗?
如何使序列化过程区域设置独立?
您知道其他可能导致此问题的原因吗?
I'm using XmlSerializer
with classes created from a xsd
using xsd.exe
. It has worked fine for months.
Now I'm receiving reports that in some cases the created xml file has all decimals serialized without a decimal point, e.g. 123.45 is serialized as 12345.
I haven't been able to reproduce the problem, but I'm suspecting it may be related to localization.
This is my first C# project, so I may be overlooking something basic.
Could localization cause this problem?
How can I make the serialization process locale independent?
Any idea of something else that could cause this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在某些国家/地区,他们使用逗号作为小数点分隔符,并使用点来分隔千位,因此您的预感可能是正确的。如果您使用的是
float
或其他保存小数的数据类型,则XmlSerializer
在任何情况下都不应该丢弃小数。但是,如果在这样的国家/地区您阅读 Xml 时未指定区域性,它可能会忽略该点。因此,如果您不指定诸如
InvariantCulture
之类的内容,则在一个国家/地区序列化并在另一个国家/地区反序列化的 Xml 会导致问题。In some countries they use a comma as decimal separator, and the dot to separate thousands, so your hunch could be true. If you're using a
float
or another data type that holds decimals,XmlSerializer
should not in any case throw away the decimals. However if in such a country you read The Xml without specifying a culture, it might ignore the dot.So Xml serialized in one country and deserialized in another country would cause problems if you don't specify something like
InvariantCulture
.