XmlSerializer用逗号(,)十进制符号反序列化十进制

编程入门 行业动态 更新时间:2024-10-27 10:30:33
本文介绍了XmlSerializer用逗号(,)十进制符号反序列化十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否存在使用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用逗号(,)十进制符号反序列化十进制

本文发布于:2023-11-08 22:16:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570596.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逗号   符号   序列化   XmlSerializer   十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!