title: IXmlSerializable dictionary in C# without 'Key'/'Value' nodes
body: <p>I'm trying to serialize a dictionary in C#.  All <a href="http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx" rel="nofollow noreferrer">the</a> <a href="http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/15b3d72a-72ae-4e74-a760-cdd6cad0f3d5" rel="nofollow noreferrer">examples</a> I've been able to find create XML like the following:</p>
  
  <pre><code>&lt;Dictionary&gt;
      &lt;ArrayOfEntries&gt;
          &lt;Entry&gt;
              &lt;Key&gt;myFirstKey&lt;/Key&gt;
              &lt;Value&gt;myFirstValue&lt;/Value&gt;
          &lt;/Entry&gt;
          &lt;Entry&gt;
              &lt;Key&gt;mySecondKey&lt;/Key&gt;
              &lt;Value&gt;mySecondValue&lt;/Value&gt;
          &lt;/Entry&gt;
      &lt;/ArrayOfEntries&gt;
  &lt;/Dictionary&gt;
  </code></pre>
  
  <p>It varies, sometimes the <code>ArrayOfEntries</code> 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:</p>
  
  <pre><code>&lt;Dictionary&gt;
      &lt;myFirstKey&gt;myFirstValue&lt;/myFirstKey&gt;
      &lt;mySecondKey&gt;mySecondValue&lt;/mySecondKey&gt;
  &lt;/Dictionary&gt;
  </code></pre>
  
  <p>I have written <code>ReadXml</code> and <code>WriteXml</code> to do this before and it works for a single dictionary, but if I try to serialize and deserialize a <code>List&lt;T&gt;</code> of my serializable dictionary instances, the deserialized list ends up with only the <em>last</em> 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 <code>List</code>'s of serializable dictionaries:</p>
  
  <pre><code>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);
          }
      }
  }
  </code></pre>
  
  <p>And <code>ReadXml</code>:</p>
  
  <pre><code>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;
          }
      }
  }
  </code></pre>
  
  <p>One difference I've noticed between other tutorials and what I'm doing is that many use <code>XmlSerializer</code> to serialize and deserialize objects in their <code>ReadXml</code> and <code>WriteXml</code>, whereas I use <code>WriteElementString</code> and the like.</p>
  
  <p>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 <code>List&lt;MySerializableDictionary&gt;</code> also works?  Even just hints of "why are you doing <em>blah</em>, that might make it act funny" would help.</p>