关于小数分隔符的问题

发布于 2024-12-04 06:15:02 字数 851 浏览 0 评论 0原文

小数点分隔符('.' 或 ',')取决于 CurrentCulture 吗?

我在序列化 XML 时遇到问题。 当我输入“,”作为分隔符时,出现异常。 (Culture设置为DE-de)

问候

示例(TestProperties是我自己的测试类)

TestProperties properties = new TestProperties 

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);

double tempValue = 1.23 // Or 1,23
properties.DoubleValue = tempValue;

XmlSerializer serializer = new XmlSerializer(typeof(TestProperties));
TextWriter textWriter = new StreamWriter(XMLPath);
serializer.Serialize(textWriter, properties);
textWriter.Close();


public class TestProperties
    {   
        private double _doubleValue;
        [XmlElement("Double")]
        public double DoubleValue
        {
            get { return _doubleValue; }
            set { _doubleValue = value; }
        }
    }

Is decimal separator ('.' or ',' ) depends of CurrentCulture?

I have a problem in serialization XML.
When I type ',' as separator, I have an exception. (Culture is setted as DE-de)

Regards

example ( TestProperties is my own class for testing)

TestProperties properties = new TestProperties 

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);

double tempValue = 1.23 // Or 1,23
properties.DoubleValue = tempValue;

XmlSerializer serializer = new XmlSerializer(typeof(TestProperties));
TextWriter textWriter = new StreamWriter(XMLPath);
serializer.Serialize(textWriter, properties);
textWriter.Close();


public class TestProperties
    {   
        private double _doubleValue;
        [XmlElement("Double")]
        public double DoubleValue
        {
            get { return _doubleValue; }
            set { _doubleValue = value; }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

骄兵必败 2024-12-11 06:15:02

小数分隔符由当前区域性确定,但是,对于 XML 序列化,不考虑当前区域性。必须使用 XML 约定; XML 中的小数点分隔符始终是点。

如果您手动构建 XML,则应使用 < code>XMLConvert 类,以确保所有数据类型在 XML 中格式正确。

Decimal separator is determined by the current culture, however, for XML Serialization, the current culture is not taken into account. XML convention will have to be used; decimal separator in XML will always be a point.

If you're building the XML by hand, you should use the XMLConvert class to make sure that all data-types are formatted correctly in the XML.

活泼老夫 2024-12-11 06:15:02

这完全取决于上下文。你提到了xml; 在 xml 中,格式通常以非文化形式表示(这意味着: . 是十进制, , 是千位等)。类似地,xml 有日期/时间的特定表示。

如果您通过 XmlWriterXElementXmlSerializer(等)构建 xml,这将是自动的;如果你手工构建它,它很可能会被混淆(不同表示的混合)。

如果数据采用预期格式,您可能必须将其加载到string属性中(而不是float等) ),并单独处理。

It depends entirely on the context. You mention xml; within xml, the format is usually represented in a non-cultural culture (which means: . is decimal and , is thousands etc). Similarly, xml has specific representations for dates/times.

If you are building your xml via XmlWriter, XElement, XmlSerializer (etc) this will be automatic; if you are building it by hand it could well be confused (a mix of different representations).

If the data is not in the expected format, you might have to load it into a string property (rather than, say, a float etc), and process it separately.

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