开发者

XmlSerializer to deserialize decimal with comma(,) decimal symbol

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; }
}

and I have xml...

string xmlExample = "<Product><Name>Coca Cola, 2L</Name><Price>3,50</Price></Product>";

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

I'am getting error There is an error in XML document (1, 57).

Everythig works fine, when Price in xml is 3.50.

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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜