Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with serializing a dictionary wrapper
    primarykey
    data
    text
    <p>I defined two classes. First one...</p> <pre><code>[Serializable] public class LocalizationEntry { public LocalizationEntry() { this.CatalogName = string.Empty; this.Identifier = string.Empty; this.Translation = new Dictionary&lt;string, string&gt;(); this.TranslationsList = new List&lt;Translation&gt;(); } public string CatalogName { get; set; } public string Identifier { get; set; } [XmlIgnore] public Dictionary&lt;string, string&gt; Translation { get; set; } [XmlArray(ElementName = "Translations")] public List&lt;Translation&gt; TranslationsList { get { var list = new List&lt;Translation&gt;(); foreach (var item in this.Translation) { list.Add(new Translation(item.Key, item.Value)); } return list; } set { foreach (var item in value) { this.Translation.Add(item.Language, item.Text); } } } } </code></pre> <p>...where <code>public List&lt;Translation&gt; TranslationsList</code> is a wrapper for non-serializable <code>public Dictionary&lt;string, string&gt; Translation</code>.</p> <p>Pair of key and value is defined as follows:</p> <pre><code>[Serializable] public class Translation { [XmlAttribute(AttributeName = "lang")] public string Language { get; set; } [XmlText] public string Text { get; set; } public Translation() { } public Translation(string language, string translation) { this.Language = language; this.Text = translation; } } </code></pre> <p>At last code used to serialize:</p> <pre><code>static void Main(string[] args) { LocalizationEntry entry = new LocalizationEntry() { CatalogName = "Catalog", Identifier = "Id", }; entry.Translation.Add("PL", "jabłko"); entry.Translation.Add("EN", "apple"); entry.Translation.Add("DE", "apfel"); using (FileStream stream = File.Open(@"C:\entry.xml", FileMode.Create)) { XmlSerializer serializer = new XmlSerializer(typeof(LocalizationEntry)); serializer.Serialize(stream, entry); } LocalizationEntry deserializedEntry; using (FileStream stream = File.Open(@"C:\entry.xml", FileMode.Open)) { XmlSerializer serializer = new XmlSerializer(typeof(LocalizationEntry)); deserializedEntry = (LocalizationEntry)serializer.Deserialize(stream); } } </code></pre> <p>The problem is that after deserialization <code>deserializedEntry.TranslationsList</code> is empty. I set a breakpoint at setter of <code>LocalizationEntry.TransalionsList</code> and it comes from deserializer empty as well. Product of serialization is of course valid. Is there any gap in my code?</p> <p>EDIT:</p> <p>Here is generated XML:</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;LocalizationEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;CatalogName&gt;Catalog&lt;/CatalogName&gt; &lt;Identifier&gt;Id&lt;/Identifier&gt; &lt;Translations&gt; &lt;Translation lang="PL"&gt;jabłko&lt;/Translation&gt; &lt;Translation lang="EN"&gt;apple&lt;/Translation&gt; &lt;Translation lang="DE"&gt;apfel&lt;/Translation&gt; &lt;/Translations&gt; &lt;/LocalizationEntry&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload