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

后端 未结 2 1409
暖寄归人
暖寄归人 2021-01-23 11:23

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

2条回答
  •  生来不讨喜
    2021-01-23 12:07

    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 people = GetPeople(); //from somewhere
    List filteredPeople = people.Where(p => !p.Deceased);
    

    Now you only need to serialise filteredPeople.

提交回复
热议问题