C# 序列化DictionaryEntry[]属性时发生XML错误
我得到一个异常消息:“XML文档中有一个错误” 代码:C# 序列化DictionaryEntry[]属性时发生XML错误,c#,xml,arrays,xml-serialization,C#,Xml,Arrays,Xml Serialization,我得到一个异常消息:“XML文档中有一个错误” 代码: private readonly SortedList\u属性; [XmlArray(“属性”)] [XmlArrayItem(“AttributesLine”,Type=typeof(DictionaryEntry))] 公共字典入口[]\u x\u属性 { 得到 { DictionaryEntry[]ret=新DictionaryEntry[\u attributes.Count]; int i=0; foreach(KeyValu
private readonly SortedList\u属性;
[XmlArray(“属性”)]
[XmlArrayItem(“AttributesLine”,Type=typeof(DictionaryEntry))]
公共字典入口[]\u x\u属性
{
得到
{
DictionaryEntry[]ret=新DictionaryEntry[\u attributes.Count];
int i=0;
foreach(KeyValuePair填充行输入_属性)
{
object value=stuffLine.value;//我假设您使用的是XMLSerializer,它无法处理通用字典对象。我看到自己在使用字典时抛出了该错误。您需要选择其他可以处理它们的序列化程序之一
请改用DataContractSerializer。谢谢,但我很难替换序列化程序。此外,它目前使用单浮点值。
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);
}
}
}