从序列化 XML 中的小数中删除小数点

发布于 2024-10-10 05:48:50 字数 310 浏览 0 评论 0原文

我将 XmlSerializer 与使用 xsd.exexsd 创建的类一起使用。几个月以来一直运行良好。

现在我收到报告,在某些情况下,创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2024-10-17 05:48:50

在某些国家/地区,他们使用逗号作为小数点分隔符,并使用点来分隔千位,因此您的预感可能是正确的。如果您使用的是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文