I have been able to serialize an IEnumerable this way:
[XmlArray("TRANSACTIONS")] [XmlArrayItem("TRANSACTION", typeof(Record))] public IEnumerable<BudgetRecord> Records { get { foreach(Record br in _budget) { yield return br; } } }
However, I realised that now I need a dictionary containing a collection Dictionary<string, RecordCollection>
(RecordCollection implements IEnumerable).
How can I achieve that?
NET objects is made easy by using the various serializer classes that it provides. But serialization of a Dictionary object is not that easy. For this, you have to create a special Dictionary class which is able to serialize itself. The serialization technique might be different in different business cases.
Unity cannot serialize standard dictionaries. This means that they won't show or be edited in the inspector and they won't be instantiated at startup. A classic workaround is to store the keys and values in separate arrays and construct the dictionary at startup.
XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.
XML serialization serializes only the public fields and property values of an object into an XML stream. XML serialization does not include type information. For example, if you have a Book object that exists in the Library namespace, there is no guarantee that it is deserialized into an object of the same type.
Take a look at the following blog post
and this one (not in english, but the code is useful)
Code sample from: http://web.archive.org/web/20100703052446/http://blogs.msdn.com/b/psheill/archive/2005/04/09/406823.aspx
using System.Collections.Generic; using System.Collections; using System.IO; using System.Xml.Serialization; using System.Xml; using System; public static void Serialize(TextWriter writer, IDictionary dictionary) { List<Entry> entries = new List<Entry>(dictionary.Count); foreach (object key in dictionary.Keys) { entries.Add(new Entry(key, dictionary[key])); } XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>)); serializer.Serialize(writer, entries); } public static void Deserialize(TextReader reader, IDictionary dictionary) { dictionary.Clear(); XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>)); List<Entry> list = (List<Entry>)serializer.Deserialize(reader); foreach (Entry entry in list) { dictionary[entry.Key] = entry.Value; } } public class Entry { public object Key; public object Value; public Entry() { } public Entry(object key, object value) { Key = key; Value = value; } }
It generates output like the following, when the keys and values are strings.
<?xml version="1.0" encoding="utf-8"?> <ArrayOfEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Entry> <Key xsi:type="xsd:string">MyKey</Key> <Value xsi:type="xsd:string">MyValue</Value> </Entry> <Entry> <Key xsi:type="xsd:string">MyOtherKey</Key> <Value xsi:type="xsd:string">MyOtherValue</Value> </Entry> </ArrayOfEntry>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With