关于小数分隔符的问题
小数点分隔符('.' 或 ',')取决于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
小数分隔符由当前区域性确定,但是,对于 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.这完全取决于上下文。你提到了xml; 在 xml 中,格式通常以非文化形式表示(这意味着: . 是十进制, , 是千位等)。类似地,xml 有日期/时间的特定表示。
如果您通过
XmlWriter
、XElement
、XmlSerializer
(等)构建 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, afloat
etc), and process it separately.