I get an exception with the message: “There is an error in XML document”.
Code:
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);
}
}
}
The value of each key/value pair is of type float[]. I still want the value type to remain ‘System.Object’ as some keys can have values of types other than float[] (In any case, I get the exception even when the dictionary is populated with one entry).
Edit to clarify: I’m using the ‘XmlSerializer’, which worked fine when entry.Value was a ‘float’.
I am assuming you are using the XMLSerializer, which can’t handle Generic Dictionary objects. I have seen that error being thrown when using Dictionaries myself. You need to choose one of the other serializers which can handle them.
Try the DataContractSerializer instead.