有条件地序列化/反序列化属性 [英] Conditionally serialize/deserialize attribute

查看:67
本文介绍了有条件地序列化/反序列化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它的对象必须根据 bool 值序列化/反序列化一个属性

I have a class whose objects must serialize/deserialize an attribute depending on a bool value

[System.SerializableAttribute()]
public class Foo
{
    private string myField;
    private bool myFieldSerializes;

    //Parameterless construction for serializing purposes
    public Foo() { }

    public Foo(string myField, bool myFieldSerializes)
    {
        this.myField = myField;
        this.myFieldSerializes = myFieldSerializes;
    }

    public string MyField
    {
        get {return this.myField;}
        set {this.myField = value;}
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool MyFieldSerializes
    {
        get {return this.myFieldSerialzes;}
        set {this.myFieldSerialzes = value;}
    }
}

它应该是这样工作的:

  • 如果我创建一个实例并将 myFieldSerializes 设置为true",则在序列化对象时(使用 XMLSerialize),必须序列化 myField(包含在 XML 消息中).如果设置为false",则应忽略.

  • If i create an instance and set myFieldSerializes to 'true', when serializing the object (with XMLSerialize), myField must be serialized (included on the XML message). If is set to 'false', it should be ignored.

反序列化(使用 XMLDeserialize)时,布尔值 myFieldSerializes 应该告诉我 myField 是否已被反序列化(换句话说,它存在于 XML 文件中).

When deserializing (with XMLDeserialize), the boolean myFieldSerializes should tell me if myField has been deserialized (in other words, it was present in the XML file).

实现这种行为的方法是什么?

What is the way to implement this behavior?

谢谢!:)

推荐答案

您的要求符合 XmlSerializerpropertyNameSpecified 模式.来自文档:

Your requirements match the propertyNameSpecified pattern of XmlSerializer. From the docs:

如果架构包含可选元素... [一个] 选项是使用特殊模式创建 XmlSerializer 识别的布尔字段,并将 XmlIgnoreAttribute 应用于该字段.该模式以propertyNameSpecified 的形式创建.例如,如果有一个名为MyFirstName"的字段,您还将创建一个名为MyFirstNameSpecified"的字段,以指示 XmlSerializer 是否生成名为MyFirstName"的 XML 元素.

If a schema includes an element that is optional ... [one] option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of propertyNameSpecified. For example, if there is a field named "MyFirstName" you would also create a field named "MyFirstNameSpecified" that instructs the XmlSerializer whether to generate the XML element named "MyFirstName".

这种模式的方便之处在于,除了记录的行为之外,在反序列化过程中,如果遇到该属性,XmlSerializer 会将 propertyNameSpecified 设置为 true —— 这正是你需要什么.因此你的类应该是这样的:

What's convenient about this pattern is that, beyond the documented behavior, during deserialization, XmlSerializer will set the propertyNameSpecified to true if the property was encountered -- which is exactly what you need. Thus your class should look like:

public class Foo
{
    private string myField;
    private bool myFieldSerializes;

    //Parameterless construction for serializing purposes
    public Foo() { }

    public Foo(string myField, bool myFieldSerializes)
    {
        this.myField = myField;
        this.myFieldSerializes = myFieldSerializes;
    }

    [XmlElement(IsNullable = true)] // Emit a value even when null as long as MyFieldSpecified == true
    public string MyField
    {
        get { return this.myField; }
        set { this.myField = value; }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool MyFieldSpecified { get { return myFieldSerializes; } set { myFieldSerializes = value; } }
}

(将 [XmlElement(IsNullable = true)] 添加到您的 MyField 属性可确保在 MyFieldSpecified == true 时始终发出元素>,即使字段本身是 null.)

(Adding [XmlElement(IsNullable = true)] to your MyField property ensures that an element will always be emitted when MyFieldSpecified == true, even if the field itself is null.)

原型 fiddle.

这篇关于有条件地序列化/反序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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