C#将十进制序列化为xml [英] C# serialize decimal to xml

查看:33
本文介绍了C#将十进制序列化为xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小数属性,比如

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆