Deserialize generic collections - coming up empty

Posted by AC on Stack Overflow See other posts from Stack Overflow or by AC
Published on 2010-04-03T11:22:42Z Indexed on 2010/04/03 11:33 UTC
Read the original article Hit count: 468

I've got a settings object for my app that has two collections in it. The collections are simple List generics that contain a collection of property bags. When I serialize it, everything is saved with no problem:

XmlSerializer x = new XmlSerializer(settings.GetType());
TextWriter tw = new StreamWriter(@"c:\temp\settings.cpt");
x.Serialize(tw, settings);

However when I deserialize it, everything is restored except for the two collections (verified by setting a breakpoint on the setters:

XmlSerializer x = new XmlSerializer(typeof(CourseSettings));
XmlReader tr = XmlReader.Create(@"c:\temp\settings.cpt");
this.DataContext = (CourseSettings)x.Deserialize(tr);

What would cause this? Everything is pretty vanilla... here's a snippet from the settings object... omitting most of it. The PresentationSourceDirectory works just fine, but the PresentationModules' setter isn't hit:

private string _presentationSourceDirectory = string.Empty;
public string PresentationSourceDirectory {
  get { return _presentationSourceDirectory; }
  set {
    if (_presentationSourceDirectory != value) {
      OnPropertyChanged("PresentationSourceDirectory");
      _presentationSourceDirectory = value;
    }
  }
}

private List<Module> _presentationModules = new List<Module>();
public List<Module> PresentationModules {
  get {
    var sortedModules = from m in _presentationModules
                        orderby m.ModuleOrder
                        select m;
    return sortedModules.ToList<Module>();
  }
  set {
    if (_presentationModules != value) {
      _presentationModules = value;
      OnPropertyChanged("PresentationModules");
    }
  }
}

© Stack Overflow or respective owner

Related posts about xml-serialization

Related posts about serialization