I have a class containing a SortedList<string, Data> as private field, where Data is a simple custom class with some int, DateTime and Nullable<DateTime> fields.
public class CustomCollection
{
private SortedList<string, Data> _list;
...
}
Now I would make my class serializable, so I could write its content (ie the items of the _list field) in an XML file or load data from an existing XML file.
How should I proceed?
I think I understand that there are two ways to serialize: the first would be to mark all fields as serializable, while the second would be to implement the IXmlSerializable interface. If I understand correctly, when I can use each of the two ways?
Ok, you just need to decorate your Classes with [Serializable] attribute and it should work. However you have a SortedList which implements an IDictionary and these cant be serialized with the IXMLSerializable so need to do a bit of customization look here
Serializing .NET dictionary
but if you change your sorted list to a normal list or anything that doesnt implement an IDictionary then the below code will work 🙂 copy it to a console app and run it.