2
我想推廣我的一個項目的序列化。序列化與C#代碼錯誤
我有三個主要類別如下:
test.cs中 - 一個簡單的測試對象
[Serializable()]
public class test : Attribute {
public string name = "";
public int ID = 0;
public test(string inputName, int inputID) {
name = inputName;
ID = inputID;
}
public test() {}
}
Serialization.cs - 我的主序列化類
public static void SerializeCollection<T>(string path, List<T> collection, Type type) {
System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(type);
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
writer.Serialize(file, collection);
}
終於Form1上。 cs - 我的表單類
private void btnSerialize_Click(object sender, EventArgs e)
{
List<test> test = new List<test>();
test.Add(new test("testing1", 2));
Serialization.SerializeCollection("Test.txt", test, typeof(test));
}
當運行並點擊按鈕,我得到這個錯誤:
'An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
Additional information: There was an error generating the XML document.'
是否有內部異常?如果是這樣,那麼它的信息是什麼? – Aybe