Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing a list of Key/Value pairs to XML
    primarykey
    data
    text
    <p>I have a list of key/value pairs I'd like to store in and retrieve from a XML file. So this task is similar as described <a href="https://stackoverflow.com/questions/284858/simplest-possible-key-value-pair-file-parsing-in-net">here</a>. I am trying to follow the advice in the marked answer (using a <strong>KeyValuePair</strong> and a <strong>XmlSerializer</strong>) but I don't get it working.</p> <p>What I have so far is a "Settings" class ...</p> <pre><code>public class Settings { public int simpleValue; public List&lt;KeyValuePair&lt;string, int&gt;&gt; list; } </code></pre> <p>... an instance of this class ...</p> <pre><code>Settings aSettings = new Settings(); aSettings.simpleValue = 2; aSettings.list = new List&lt;KeyValuePair&lt;string, int&gt;&gt;(); aSettings.list.Add(new KeyValuePair&lt;string, int&gt;("m1", 1)); aSettings.list.Add(new KeyValuePair&lt;string, int&gt;("m2", 2)); </code></pre> <p>... and the following code to write that instance to a XML file:</p> <pre><code>XmlSerializer serializer = new XmlSerializer(typeof(Settings)); TextWriter writer = new StreamWriter("c:\\testfile.xml"); serializer.Serialize(writer, aSettings); writer.Close(); </code></pre> <p>The resulting file is:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;simpleValue&gt;2&lt;/simpleValue&gt; &lt;list&gt; &lt;KeyValuePairOfStringInt32 /&gt; &lt;KeyValuePairOfStringInt32 /&gt; &lt;/list&gt; &lt;/Settings&gt; </code></pre> <p>So neither key nor value of the pairs in my list are stored though the number of elements is correct. Obviously I am doing something basically wrong. My questions are:</p> <ul> <li>How can I store the key/value pairs of the list in the file?</li> <li>How can I change the default generated name "KeyValuePairOfStringInt32" of the elements in the list to some other name like "listElement" I'd like to have?</li> </ul>
    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.
 

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