序列化DictionaryEntry[]属性时出现XML错误
本文关键字:XML 错误 属性 DictionaryEntry 序列化 | 更新日期: 2023-09-27 18:28:40
我得到一个异常消息:"XML文档中有错误"。
代码:
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",它在输入时运行良好。值为"float"。
我假设您使用的是XMLSerializer,它不能处理Generic Dictionary对象。我自己在使用Dictionaries时看到过这个错误。您需要选择一个能够处理它们的其他序列化程序。
请尝试DataContractSerializer。