For a list script, there may simply be another property, so instead:
[DataMember] public List<Whatever> Items {get {...}}
you have:
public List<Whatever> Items {get {...}} [DataMember] public List<Whatever> SelectedItems { get { return Items.FindAll(x => x.IsSelected); }
however, deserializing , which would be painful, since your list would have to load in Items; if you need to also deserialize, you may need to write a complex user list.
As a second idea; just create a second instance of the object with just the elements you want to serialize; very simple and efficient:
var dto = new YourType { X = orig.X, Y = orig.Y, ... }; foreach(var item in orig.Items) { if(orig.Items.IsSelected) dto.Items.Add(item); }
AFAIK, DataContractSerializer does not support conditional serialization of members.
At the individual property level, this is an option if you are using an XmlSerializer , although for a property, say, Foo , you simply add:
public bool ShouldSerializeFoo() {
or
[XmlIgnore] public bool FooSpecified { get { } set { } }
They are used solely as a name-based agreement.
Marc gravell
source share