由XmlSerializer的被发射燮preSS空值类型 [英] Suppress Null Value Types from Being Emitted by XmlSerializer

查看:122
本文介绍了由XmlSerializer的被发射燮preSS空值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑其标记为可为空的XmlElement以下金额值类型属性:

Please consider the following Amount value type property which is marked as a nullable XmlElement:

[XmlElement(IsNullable=true)] 
public double? Amount { get ; set ; }

在可空类型设置为null,则C#XmlSerializer的结果如下所示:

When a nullable value type is set to null, the C# XmlSerializer result looks like the following:

<amount xsi:nil="true" />

而不是发光这个元素,我想XmlSerializer的完全燮preSS的元素。为什么?我们使用Authorize.NET进行在线支付,如果这个空元素存在Authorize.NET拒绝该请求。

Rather than emitting this element, I would like the XmlSerializer to suppress the element completely. Why? We're using Authorize.NET for online payments and Authorize.NET rejects the request if this null element exists.

目前的解决方案/解决方法是不是在所有序列化的数量值类型属性。相反,我们已经创建了一个互补的特性,SerializableAmount,这是基于量,转而系列化。由于SerializableAmount是String类型,类似的参考类型,如果为空默认情况下由XmlSerializer的pssed燮$ P $的,一切都很正常。

The current solution/workaround is to not serialize the Amount value type property at all. Instead we have created a complementary property, SerializableAmount, which is based on Amount and is serialized instead. Since SerializableAmount is of type String, which like reference types are suppressed by the XmlSerializer if null by default, everything works great.

/// <summary>
/// Gets or sets the amount.
/// </summary>
[XmlIgnore]
public double? Amount { get; set; }

/// <summary>
/// Gets or sets the amount for serialization purposes only.
/// This had to be done because setting value types to null 
/// does not prevent them from being included when a class 
/// is being serialized.  When a nullable value type is set 
/// to null, such as with the Amount property, the result 
/// looks like: &gt;amount xsi:nil="true" /&lt; which will 
/// cause the Authorize.NET to reject the request.  Strings 
/// when set to null will be removed as they are a 
/// reference type.
/// </summary>
[XmlElement("amount", IsNullable = false)]
public string SerializableAmount
{
    get { return this.Amount == null ? null : this.Amount.ToString(); }
    set { this.Amount = Convert.ToDouble(value); }
}

当然,这只是一种变通方法。有没有被发出共进晚餐preSS空值类型元素的更清洁的方式?

Of course, this is just a workaround. Is there a cleaner way to suppress null value type elements from being emitted?

推荐答案

尝试添加:

public bool ShouldSerializeAmount() {
   return Amount != null;
}

有若干由框架的部件识别图案。对于信息,的XmlSerializer 也查找公共BOOL AmountSpecified {获取;集;}

There are a number of patterns recognised by parts of the framework. For info, XmlSerializer also looks for public bool AmountSpecified {get;set;}.

完整的示例(也切换到小数

Full example (also switching to decimal):

using System;
using System.Xml.Serialization;

public class Data {
    public decimal? Amount { get; set; }
    public bool ShouldSerializeAmount() {
        return Amount != null;
    }
    static void Main() {
        Data d = new Data();
        XmlSerializer ser = new XmlSerializer(d.GetType());
        ser.Serialize(Console.Out, d);
        Console.WriteLine();
        Console.WriteLine();
        d.Amount = 123.45M;
        ser.Serialize(Console.Out, d);
    }
}

在MSDN上 ShouldSerialize

更​​多信息*。

More information on ShouldSerialize* on MSDN.

这篇关于由XmlSerializer的被发射燮preSS空值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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