English 中文(简体)
为什么XmlSerializer扔下了一种无效的行动。
原标题:Why is XmlSerializer throwing an InvalidOperationException?
    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
          */

          // ....
     }

如果需要的话,那是整类:

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:如果你有改进我守则的任何捷径,我可以自由分享,我是C#的开端人)

最佳回答

序列化/代号化其需要无参数建筑商的类型。 查阅

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

问题回答

oh。 我不知道它有其他信息(而是要点击“意见细节”),神秘的解决办法是:

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

我也看到了这一例外,但由于缺乏违约建筑商,这一例外是没有的。 我有一些额外特性(<代码>List 和>Dictionary),它们是XML文件的一部分。

用<代码>[XmlIgnore]来决定这些财产,解决了我的问题。

你们可以通过提供一名称为超负荷施工者的违约建筑商来解决这一问题。 例如:

public DatabaseInformation() : this ("defaultName"){}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签