简体   繁体   English

C#将十进制序列化为xml

[英]C# serialize decimal to xml

I got a decimal property, like我有一个小数属性,比如

[XmlElementAttribute(DataType = "decimal")] decimal Price

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> .问题是我想强制它总是以 2 的精度序列化,但如果价格是 10.50,它将被序列化为 XML,如<Price>10.5</Price>

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 ?有什么方法可以强制它(不创建新属性或更改此属性的获取?我正在寻找一些方法来做到这一点,只将模式发送到 XmlSerializer(或 XmlElementAttribute)或任何聪明的方法来做到这一点?

Thanks谢谢

I was having the opposite problem.我遇到了相反的问题。 My decimals were serializing with 4 decimal places, even though they were all 4 zeroes.我的小数用 4 个小数位进行序列化,即使它们都是 4 个零。 I discovered that if I call decimal.Round(value, 2) then it serializes to 2 decimal places.我发现如果我调用decimal.Round(value, 2)那么它会序列化到 2 个小数位。 It would appear that the Decimal type remembers what you last rounded it too when it is serialized.看起来 Decimal 类型在序列化时也会记住您上次对其进行四舍五入的内容。

I was suspicious of the suggestion, but it worked that simply.我对这个建议持怀疑态度,但它很简单。 Even though the value didn't need rounding, calling Round changed how many decimal places showed up in serialization.尽管该值不需要四舍五入,但调用 Round 会改变序列化中出现的小数位数。

You could add XmlIgnore to the actual decimal property and introduce a new property PriceAsString which returns, eh, the price as string (in 10.5 0 format).您可以将XmlIgnore添加到实际的十进制属性并引入一个新属性 PriceAsString,它以字符串形式返回,嗯,价格(10.5 0格式)。

You could of course also implement IXmlSerializable and do everything yourself.您当然也可以实现IXmlSerializable并自己做所有事情。

However, none of these ways really rocks, and you already stated you were not going to go down this road anyway...然而,这些方式都不是真正的摇滚,你已经说过无论如何你都不会走这条路……

I had a few int s that I assigned to decimal properties (of the object-to-serialize).我有几个int分配给decimal属性(要序列化的对象)。 They needed to be written with 2 decimals by the XML Serializer, but came out without decimals. XML Serializer 需要用 2 个小数写入它们,但结果却没有小数。

Assuming假设

var myInt = 3;

No success:没有成功:

Setting a property that needs serialization via设置需要通过序列化的属性

obj.Property = decimal.Round((decimal)myInt, 2);

results in no decimals being written at serialization.导致在序列化时不写入小数。

Success:成功:

With

obj.Property = decimal.Parse(d.ToString("n2", CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture);

I get two decimals.我得到两位小数。

Now I now use these extension methods at setting the properties:现在我在设置属性时使用这些扩展方法:

/// <summary>
/// For XML serialization.
/// </summary>
public static class DecimalSerializationHelper
{
    public static decimal WithTwoDecimals(this decimal d)
        => decimal.Parse(d.ToString("n2", CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture);

    public static decimal WithTwoDecimals(this int x) => ((decimal)x).WithTwoDecimals();
}

so that it reads like所以它看起来像

obj.Property = myInt.WithTwoDecimals();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM