0

Here's a C# class I'm serializing to XML, currently with System.Xml.Serialization.XmlSerializer:

public class Stuff
{
    // ...

    [XmlElement("things")]
    public List<Thing> Things { get; set; }
}

Is there any way to control, at serialization time, whether or not to serialize each individual Thing in the list? (For example, calling a predicate on each Thing to determine whether or not it should be included.)

Note: I've read a lot about the "ShouldSerialize*" trick from other questions, but that seems to apply only at the property level — that is, serialize the whole list, or none of it. What I want is to decide that for each individual member of my list. Solution doesn't have to use XmlSerializer if there's a better way to go about it.

Community
  • 1
  • 1
user1454265
  • 868
  • 11
  • 25

1 Answers1

0

Serialization is about serializing an entire object, with the possible exception of certain of the properties of the object.

If you want individual elements of the list to not be serialized, then don't put them in the list. Possibly create a copy of the list, but with the undesirable elements removed.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Thanks, that works for me, I guess. The distinction between having control over properties but not list members feels kind of arbitrary to me, but seems reasonable if it's just not a feature that's needed very often. – user1454265 Dec 02 '14 at 22:28