0

assuming the following XML structure:

<ROOT>
    <ELEMENT>...</ELEMENT>   
    <ELEMENT>...</ELEMENT>
    <ANOTHERELEMENT>...</ANOTHERELEMENT>
</ROOT>

I am deserializing all XML elements of type ELEMENT into a List<ElementType> via the following way:

[XmlElement("ELEMENT")]
public List<ElementType> Elements { get; set; }

So far so good, but now I need to control which objects of type ElementType to serialize. This should be achieved by checking the objects' IsEnabled property. Only objects with IsEnabled = true should be serialized. This property is not part of the XML document but can be set via the program's GUI. As far as I can see, ShouldSerialize[MemberName] won't do the trick in this case.

Initially, I was thinking about using some sort of adaptor property but wanted to see if there is an elegant way of achieving my goal.

Unfortunately, making changes to the XML structure is not really an alternative.

Thanks in advance, guys. Any help will be greatly appreciated. Cheers, Zettel.

Zettel
  • 3
  • 3
  • [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Amit Kumar Ghosh Mar 01 '16 at 06:13
  • I don't think there's any built-in way to do this. In [How to ignore a specific List item when serializing a container class](https://stackoverflow.com/questions/33786527/how-to-ignore-a-specific-listt-item-when-serializing-a-container-class) the suggestion was, essentially, to serialize an adaptor property as you suggest. Note it needs to be an *array* not a list, otherwise the serializer will set it back before populating it. – dbc Mar 02 '16 at 18:29

0 Answers0