C#将十进制序列化为xml

编程入门 行业动态 更新时间:2024-10-27 16:29:31
本文介绍了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

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

发布评论

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

>www.elefans.com

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