I'm trying to serialize a dictionary in C#. All the examples I've been able to find create XML like the following:
<Dictionary>
<ArrayOfEntries>
<Entry>
<Key>myFirstKey</Key>
<Value>myFirstValue</Value>
</Entry>
<Entry>
<Key>mySecondKey</Key>
<Value>mySecondValue</Value>
</Entry>
</ArrayOfEntries>
</Dictionary>
It varies, sometimes the ArrayOfEntries
node isn't necessary, but I still see the regular pattern of the dictionary's key-value pairs being stored in their own nodes. What I would like is something like the following:
<Dictionary>
<myFirstKey>myFirstValue</myFirstKey>
<mySecondKey>mySecondValue</mySecondKey>
</Dictionary>
I have written ReadXml
and WriteXml
to do this before and it works for a single dictionary, but if I try to serialize and deserialize a List<T>
of my serializable dictionary instances, the deserialized list ends up with only the last serializable dictionary in it. I think something must be greedy about my read or write method such that it doesn't know when to stop. Here are my serializable dictionary methods that currently don't work for serialization of List
's of serializable dictionaries:
public void WriteXml(XmlWriter writer)
{
foreach (string key in getKeysToSerialize())
{
string cleanKey = key.SanitizeForXml();
string value = getValueForKey(key).Trim();
if (isCdata(key, value))
{
string cdataFriendlyValue = cleanStringForUseInCdata(value);
if (string.IsNullOrEmpty(cdataFriendlyValue))
{
continue;
}
writer.WriteStartElement(cleanKey);
writer.WriteCData(cdataFriendlyValue);
writer.WriteEndElement();
}
else
{
writer.WriteElementString(cleanKey, value);
}
}
}
And ReadXml
:
public void ReadXml(XmlReader reader)
{
string key = null, value = null;
bool wasEmpty = reader.IsEmptyElement;
while (reader.Read())
{
if (wasEmpty)
{
return;
}
switch (reader.NodeType)
{
case XmlNodeType.Element:
key = reader.Name;
if (keyIsSubTree(key))
{
using (XmlReader subReader = reader.ReadSubtree())
{
storeSubtree(key, subReader);
}
// Reset key to null so we don't try to parse it as
// a regular key-value pair later
key = null;
}
break;
case XmlNodeType.Text:
value = reader.Value;
break;
case XmlNodeType.CDATA:
value = cleanCdataForStoring(reader.Value);
break;
case XmlNodeType.EndElement:
if (!string.IsNullOrEmpty(key))
{
string valueToStore =
string.IsNullOrEmpty(value) ? string.Empty : value
Add(key, valueToStore);
}
key = null;
value = null;
break;
}
}
}
One difference I've noticed between other tutorials and what I'm doing is that many use XmlSerializer
to serialize and deserialize objects in their ReadXml
and WriteXml
, whereas I use WriteElementString
and the like.
My question is, how can I serialize a dictionary such that its XML is like my second XML example above, but so that serializing and deserializing a List<MySerializableDictionary>
also works? Even just hints of "why are you doing blah, that might make it act funny" would help.