我想弄清楚如何做到這一點。到目前爲止,我用這種方法序列化對象的列表:如何正確序列化和反序列化字典的值?
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyInfo));
using (var writer = new StreamWriter("SerializedValues.xml"))
{
foreach (var o in dic.Values)
serializer.Serialize(writer, o);
}
它變成單獨的XML文件的列表,每一個都有自己的頭,但都在一個文件中。我認爲這是不對的。
然後我嘗試反序列化它,它給了我下面的錯誤:
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 5, position 17.
而這正是下一個XML頭開始的地方。所以我想我的序列化是錯誤的。
還有一個問題來自事實,反序列化返回一個單一的對象,這已經聽起來不對,因爲我期待着一個排序列表。
如何正確地序列化它?
'System.NotSupportedException:類型System.Collections.Generic.Dictionary''2 [[System.String,誇誇其談],[MyProject.MyInfo,誇誇其談]是不支持,因爲它實現了IDictionary.' – user1306322