13
    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;
}

(PS: if you have any tips to improve my code feel free to share, I'm a C# beginner)

qster
  • 1,129
  • 3
  • 9
  • 11
  • Could you paste exception messages here? – Andrew Bezzub Mar 23 '10 at 07:34
  • 1
    Please post complete exception information, including inner exceptions - and the according documentation texts (i.e. message). You really miss the message which often contains more information. – TomTom Mar 23 '10 at 07:34

4 Answers4

19

To serialize/deserialize your type it needs to have parameterless constructor. Check out here :

A class must have a default constructor to be serialized by XmlSerializer.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73
  • 8
    My type has parameterless constructor and I still got this error. It turns out the cause was a public property with type Uri which does not have parameterless constructor. So besides your type,also your public properties in that type also must have parameterless constructor. – user850010 Nov 17 '11 at 10:48
  • When I caught this exception and examined several levels of InnerException associated with it, I found one of my nested objects had a member whose type is an interface (IEnumerable) and this is not serializable. I guess I'll have to convert this to a concrete type. – Neek Apr 25 '16 at 08:33
6

oh.. I didn't know it had additional information (had to click "View detail.."), mystery solved:

Message=SDB.DatabaseInformation cannot be serialized because it does not have a parameterless constructor.

qster
  • 1,129
  • 3
  • 9
  • 11
1

I was also getting this exception, but it wasn't due to missing a default constructor. I had some extra properties (a List and Dictionary) which aren't part of the XML document.

Decorating those properties with [XmlIgnore] solved the problem for me.

Dave Andersen
  • 5,337
  • 3
  • 30
  • 29
0

You can get around this by providing a default constructor that calls the overloaded constructor. For example:

public DatabaseInformation() : this ("defaultName"){}
user1205577
  • 2,388
  • 9
  • 35
  • 47