Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is by design - each key in a <code>Hashtable</code> is unique, and assigning a value to a key that already exists will overwrite that value when using the indexing operator <code>[]</code>.</p> <p>I would suggest a couple of changes:</p> <p>First, avoid using <code>Hashtable</code> altogether if possible, and instead use one of the generic collections e.g. <code>Dictionary&lt;,&gt;</code>.</p> <p>If you do in fact need multiple values per key, you can probably use something along the following lines:</p> <pre><code>var dict = new Dictionary&lt;string, List&lt;string&gt;&gt;(); foreach (XmlNode node in xnode) { var key = node.Attributes["key"].Value; List&lt;string&gt; values; if (!dict.TryGetValue(key, out values)) { values = new List&lt;string&gt;(); dict.Add(key, values); } values.Add(node.Attributes["value"].Value); } </code></pre> <p>Or to use Linq to construct the dictionary, you can use something like:</p> <pre><code>var dict = xnode.Cast&lt;XmlNode&gt;() .GroupBy(n =&gt; n.Attributes["key"].Value, n =&gt; n.Attributes["value"].Value) .ToDictionary(g =&gt; g.Key, g =&gt; g.ToList()); </code></pre> <p>If you absolutely have to use <code>Hashtable</code> then the above can be adapted as follows:</p> <pre><code>var hash = new Hashtable(); foreach (XmlNode node in xnode) { var key = node.Attributes["key"].Value; var values = (List&lt;string&gt;)hash[key]; if (values == null) { values = new List&lt;string&gt;(); hash.Add(key, values); } values.Add(node.Attributes["value"].Value); } </code></pre>
 

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