我想知道是否存在使用XmlSerialier用逗号十进制分隔符反序列化十进制的简单方法? 我正在从其他软件中获取xml格式的导出数据,并且xml文件中的所有数字(价格,数量,折扣以及更多)都用逗号作为小数点分隔符。
I'd like to know if there is any simple way to deserialize decimal with comma decimal separator using XmlSerialier? I'am getting exported data in xml format from other software and all numbers(price, quantity, discount and many many more) in xml file has comma as decimal separator.
以下是我尝试做的简化示例:
Here is simplified example what I have tried to do:
我们说有名称和价格的产品类:
Lets say there is Product class with Name and Price:
[Serializable] public class Product { public string Name { get; set; } public decimal Price { get; set; } }我有xml ...
string xmlExample = "<Product><Name>Coca Cola, 2L</Name><Price>3,50</Price></Product>";当我尝试反序列化该xml ...
when I'am trying to deserialize that xml...
XmlSerializer serializer = new XmlSerializer(typeof(Product)); StringReader stringReader = new StringReader(xmlExample); Product product = serializer.Deserialize(stringReader) as Product; //<-- Error here我遇到错误 XML文档(1,57)。
当xml中的价格为3.50时,Everythig都可以正常工作。
Everythig works fine, when Price in xml is 3.50.
我知道我可以将价格属性更改为sting和TryParse十进制,但是也许有更好的解决方案(例如切换区域性)?
I know i could change Price propertie to sting and TryParse decimal, but maybe there is better solution (for example switch culture)?
任何建议,解决方案和评论
Any suggestions, solutions and comments are appreciated.
更新: 忘了说我当前的区域性设置已经使用逗号作为小数点符号了。
UPDATE: Forgot to mention that my current culture settings already using comma as decimal symbol.
推荐答案我怀疑是否存在一种内置方式,因为带有不使用句点的数字的XML文档不是XML文档:) OTOH,答案有很多可能的解决方案:
I doubt there is a built-in way because XML documents with numbers that don't use period are not XML documents :) OTOH, this answer has quite a few possible solutions:
Summing numbers with comma as decimal separator in XSLT?
更多推荐
XmlSerializer用逗号(,)十进制符号反序列化十进制
发布评论