2012-03-21 184 views
0

我收到一條異常消息:「XML文檔中存在錯誤」。XML錯誤序列化DictionaryEntry []屬性

代碼:

 private readonly SortedList<string, object> _attributes; 

     [XmlArray("Attributes")] 
     [XmlArrayItem("AttributesLine", Type=typeof(DictionaryEntry))] 
     public DictionaryEntry[] _x_Attributes 
     { 
      get 
      { 
       DictionaryEntry[] ret = new DictionaryEntry[_attributes.Count]; 
       int i=0; 
       foreach (KeyValuePair<string, object> stuffLine in _attributes) 
       { 
        object value = stuffLine.Value;  // <--- float[]   
        ret[i++] = new DictionaryEntry {Key = stuffLine.Key, Value = value};     
       } 
       return ret; 
      } 
      set 
      { 
       _attributes.Clear(); 
       foreach (DictionaryEntry entry in value) 
       { 
        _attributes.Add((string) entry.Key, entry.Value); 
       } 
      } 
     } 

每個鍵/值對的值的類型爲float []的。我仍然希望值類型保持爲'System.Object',因爲某些鍵可以具有除float []以外的其他類型的值。(無論如何,即使字典填充了一個條目,我也會得到異常)。

編輯澄清:我使用'XmlSerializer',它在entry.Value是'float'時工作正常。

回答

0

我假設您使用XMLSerializer,它無法處理通用字典對象。當我自己使用字典時,我看到了這個錯誤。您需要選擇其中一個可以處理它們的序列化程序。

嘗試使用DataContractSerializer代替。

+0

謝謝,但我很難替換序列化程序。此外,它目前與單個浮點值一起工作。 – alhazen 2012-03-22 16:34:27