如何不根据属性的值序列化对象? [英] How to not serialize an object based on a property's value?

查看:42
本文介绍了如何不根据属性的值序列化对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果标签没有泄露,我正在使用 C# 的 XmlSerializer 类.

例如,我有一个 Person 类,该类具有各种属性,包括年龄 (int)、姓名 (string) 和死者 (bool).有没有办法指定我不想序列化任何已死标志为真的对象?

我应该指定,但不幸的是,由于这种情况,我无法真正编辑我的对象列表,因为它是另一个类的成员,这正是我实际序列化的对象.还有其他建议吗?

解决方案

假设您具有以下类型的类结构(如您在评论中指定的)

公共类人{公共整数年龄{得到;放;}公共字符串名称{获取;放;}公共布尔死者{得到;放;}}公开课存在{公共字符串数据{获取;放;}[XmlElement("人类")]公共人人{得到;放;}public bool ShouldSerializeHuman(){返回 !this.Human.Deceased;}}

这里我添加了一个名为 ShouldSerialize 的方法,它被称为 XML 序列化的模式.在这里,您可以将 XmlArrayXmlArrayItem 用于列表等(使用给定名称)然后 ShouldSerialize 检查它是否可以序列化.

以下是我用于测试的代码.

 private static void Main(string[] args){var livingHuman = new Person() { Age = 1, Name = "John Doe", Deceased = true };var deadHuman = new Person() { Age = 1, Name = "John Doe", Deceased = false };XmlSerializer 序列化器 = new XmlSerializer(typeof(Being));serializer.Serialize(Console.Out, new Being { Human = livingHuman, Data = "new" });serializer.Serialize(Console.Out, new Being { Human = deadHuman, Data = "old" });}

<块引用>

这是输出:

==============================

<块引用>

更新:

如果您有 Person as Humans 列表:

公共类Being{//[XmlAttribute]公共字符串数据{获取;放;}//这里给属性添加以下属性[XmlArray("人类")][XmlArrayItem("人类")]公共列表<人>人类{得到;放;}public bool ShouldSerializeHumans(){this.Humans = this.Humans.Where(x => !x.Deceased).ToList();返回真;}}

<块引用>

样品测试:

 private static void Main(string[] args){var livingHuman = new Person() { Age = 1, Name = "John Doe", Deceased = true };var deadHuman = new Person() { Age = 1, Name = "John Doe", Deceased = false };var human = new List{ 活人,死人 };XmlSerializer 序列化器 = new XmlSerializer(typeof(Being));serializer.Serialize(Console.Out, new Being() { Humans = human, Data = "some other data" });}

<块引用>

输出:

If the tags didn't give it away, I'm working with C#'s XmlSerializer class.

Say, for example, I have a Person class with various properties including age (int), name (string), and deceased (bool). Is there a way to specify that I don't want to serialize any objects whose deceased flags are true?

Edit: I should have specified, but unfortunately due to the situation I can't really edit my list of objects because it's a member of another class, which is what I'm actually serializing. Are there any other suggestions?

解决方案

Assuming that you have following type of Class structure(As you specified in the comment)

public class Person 
{
    public int Age { get; set; }

    public string Name { get; set; }

    public bool Deceased { get; set; }
}

public class Being
{
    public string Data { get; set; }

    [XmlElement("Human")]
    public Person Human { get; set; }

    public bool ShouldSerializeHuman()
    {
        return !this.Human.Deceased;
    }
}

Here I have added a method called ShouldSerialize this is called a pattern for XML serialization. Here you can use XmlArray and XmlArrayItem for lists etc.(With given name) then the ShouldSerialize checks if it can be serialized.

Below is the code I used for testing.

    private static void Main(string[] args)
    {
        var livingHuman = new Person() { Age = 1, Name = "John Doe", Deceased = true };
        var deadHuman = new Person() { Age = 1, Name = "John Doe", Deceased = false };

        XmlSerializer serializer = new XmlSerializer(typeof(Being));

        serializer.Serialize(Console.Out, new Being { Human = livingHuman, Data = "new" });

        serializer.Serialize(Console.Out, new Being { Human = deadHuman, Data = "old" });
    }

And here's the output:

=============================

Update:

If you have list of Person as Humans:

public class Being
{
    // [XmlAttribute]
    public string Data { get; set; }

    // Here add the following attributes to the property
    [XmlArray("Humans")]
    [XmlArrayItem("Human")]
    public List<Person> Humans { get; set; }

    public bool ShouldSerializeHumans()
    {
        this.Humans = this.Humans.Where(x => !x.Deceased).ToList();
        return true;
    }
}

Sample Test:

    private static void Main(string[] args)
    {
        var livingHuman = new Person() { Age = 1, Name = "John Doe", Deceased = true };
        var deadHuman = new Person() { Age = 1, Name = "John Doe", Deceased = false };

        var humans = new List<Person> { livingHuman, deadHuman };
        XmlSerializer serializer = new XmlSerializer(typeof(Being));

        serializer.Serialize(Console.Out, new Being() { Humans = humans, Data = "some other data" });
    }

Output:

这篇关于如何不根据属性的值序列化对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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