2012-03-21 185 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