根据他们的价值与XmlSerializer的我怎么能忽略的属性 [英] How can I ignore properties according to their value with XmlSerializer

查看:113
本文介绍了根据他们的价值与XmlSerializer的我怎么能忽略的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用XmlSerializer的创造排除属性,如果他们有一个默认值的XML。这可能与XmlSerializer的还是我将不得不考虑的IXmlSerializable?

I'd like the XML created by XmlSerializer to exclude properties if they have a default value. Is this possible with XmlSerializer or am I going to have to look into IXmlSerializable?

例如,我可能有下面的类:

For example, I may have the following class:

public class PositionedObject
{
   public float X
   { get; set; }

   public float Y
   { get; set;}
}

我想告诉XmlSerializer的,当它系列化PositionedObject的实例,不包括X如果该值为0(与同为Y,如果是0)。

I'd like to tell the XmlSerializer that when it serializes an instance of PositionedObject, to not include X if the value is 0 (and same with Y if it is 0).

推荐答案

只是声明 ShouldSerializeX 返回true命名的方法,当值不为0:

Just declare a method named ShouldSerializeX that returns true when the value is not 0:

public bool ShouldSerializeX()
{
    return X != 0;
}

串行器会调用这个方法来决定属性是否应被序列化与否。

The serializer will call this method to decide whether the property should be serialized or not.

这篇关于根据他们的价值与XmlSerializer的我怎么能忽略的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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