我有一个小数属性,比如
I got a decimal property, like
[XmlElementAttribute(DataType = "decimal")] 十进制价格
问题是我想强制它总是以 2 的精度序列化,但如果价格是 10.50,它将被序列化为 XML,如 10.5.
The problem is that I wanna force it to serialize always with precision of 2, but if the price is 10.50 it will be serialized to XML like <Price>10.5</Price>.
有什么方法可以强制它(不创建新属性或更改此属性的获取?我正在寻找某种方法来执行此操作,仅将模式发送到 XmlSerializer(或 XmlElementAttribute)或任何智能方式这样做吗?
Theres any way to force it (without creating a new property or changing the get of this property? I'm looking for some way to do this only sending a pattern to the XmlSerializer (or the XmlElementAttribute) or any smart way to do this ?
谢谢
推荐答案您可以将 XmlIgnore 添加到实际的十进制属性并引入一个新属性 PriceAsString,它返回,呃,价格作为字符串(在10.50 格式).
You could add XmlIgnore to the actual decimal property and introduce a new property PriceAsString which returns, eh, the price as string (in 10.50 format).
您当然也可以实现 IXmlSerializable 并自己完成所有事情.
You could of course also implement IXmlSerializable and do everything yourself.
然而,这些方法都不是真的,而且你已经说过无论如何你都不会走这条路......
However, none of these ways really rocks, and you already stated you were not going to go down this road anyway...
更多推荐
C#将十进制序列化为xml
发布评论