none
Exception while serializing ObservableCollection

    Question

  • I have a pretty simple class called Application that is contained in an ObservableCollection array. However when I try to serialize the array I get the following exception:

    {"There was an error generating the XML document."} and the InnerException says:

    {"System.Windows.Media.Imaging.InteropBitmapSource is inaccessible due to its protection level. Only public types can be processed."}

    My class is defined as such:

    public class AppListItem : INotifyPropertyChanged

    {

    private string _name;

    private string fullpath;

    [NonSerialized]

    private BitmapSource _icon;

    ... appropriate Getters/Setters below are public

    ... implementation of INotifyPropertyChanged interface as well

    }

    This is the array definition

    public class AppList : ObservableCollection<AppListItem>

    {

    public AppList()

    : base()

    {

    }

    }

    And here is my code to do the serialization:

    string filename = App.strCurrDirectory + "\\Applist.xml";

    XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<AppListItem>));

    Stream fs = new FileStream(filename, FileMode.Create);

    XmlWriter writer = new XmlTextWriter(fs, Encoding.UTF8);

    //Serialize using the XmlTextWriter.

    xs.Serialize(writer, UnAcceleratedApps);

    writer.Close();

    I thought that by declaring the BitmapSource member as NonSerialized it would cause the XML serialization to  skip that object but the rest would be serialized. I don't much care about serializing the bitmap I just want to serialize the strings. Any ideas what is going wrong?

    Tuesday, March 20, 2007 5:38 PM

Answers

  • I've solved it. It was a simple matter of putting the [XmlIgnore] attribute around the public form of the BitmapSource member.
    Tuesday, March 20, 2007 6:13 PM

All replies