Why is XmlSerializer throwing an InvalidOperationException?

Posted by qster on Stack Overflow See other posts from Stack Overflow or by qster
Published on 2010-03-23T07:31:14Z Indexed on 2010/03/23 7:33 UTC
Read the original article Hit count: 987

    public void Save() {
          XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation));
          /*
          A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
          A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
          A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
          */

          // ....
     }

This is the whole class if you need it:

public class DatabaseInformation
{
    /* Create new database */
    public DatabaseInformation(string name) {
        mName = name;
        NeedsSaving = true;
        mFieldsInfo = new List<DatabaseField>();
    }

    /* Read from file */
    public static DatabaseInformation DeserializeFromFile(string xml_file_path)
    {
    XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation));
        TextReader r = new StreamReader(xml_file_path);
        DatabaseInformation ret = (DatabaseInformation)Serializer.Deserialize(r);
        r.Close();
        ret.NeedsSaving = false;
        return ret;
    }

    /* Save */
    public void Save() {
    XmlSerializer Serializer = new XmlSerializer(typeof(DatabaseInformation));
        if (!mNeedsSaving)
            return;

        TextWriter w = new StreamWriter(Path.Combine(Program.MainView.CommonDirectory.Get(), Name + ".xml"), false);
        Serializer.Serialize(w, this);
        w.Close();
        NeedsSaving = false;
    }

    private string mName;
    public string Name { get { return mName; } }

    private bool mNeedsSaving;
    public bool NeedsSaving { get { return mNeedsSaving; } set { mNeedsSaving = value; Program.MainView.UpdateTitle(value); } }

    private bool mHasId;
    public bool HasId { get { return mHasId; } }

    List<DatabaseField> mFieldsInfo;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception