简体   繁体   中英

How to not serialize an object based on a property's value?

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: 在此处输入图片说明

If you have a list of Person objects and only want to serialise some of them, then just filter out the ones you don't need. For example:

List<Person> people = GetPeople(); //from somewhere
List<Person> filteredPeople = people.Where(p => !p.Deceased);

Now you only need to serialise filteredPeople .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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