title: How to serialize object with List inside in C# using XElement?
body: <p>I have object with members of different types like this:</p>
  
  <pre><code>public class MyObject
  {
      public string Str1 = string.Empty;
      public MyEnums.Enum1 E1 = MyEnums.Enum1.Unknown;
      public bool Done = false;
  };
  </code></pre>
  
  <p>I have <code>Dictionary</code> of these objects:</p>
  
  <pre><code>Dictionary&lt;string, MyObject&gt; MyObjectsDic = new Dictionary&lt;string, MyObject&gt;();
  </code></pre>
  
  <p>And serializer for it like this:</p>
  
  <pre><code>public static void ToXml(string file, string collectionName, Dictionary&lt;string, object&gt; collection)
  {
      XElement root = new XElement(collectionName);
  
      root.Add(collection.Select(x =&gt; new XElement("Item", new XAttribute("Object", x.Key),
              x.Value.GetType().GetFields().Select(f =&gt; new XElement(f.Name, f.GetValue(x.Value))))));
  
      root.Save(file);
  }
  </code></pre>
  
  <p>The serializer tooks abstract <code>Dictionary</code> as argument and I need to convert my <code>MyObjectsDic</code> manually. May be I mistaken here.</p>
  
  <pre><code>ToXml("MyFile.xml", "MyObjects", MyObjectsDic.ToDictionary(p =&gt; p.Key, p =&gt; (object)p.Value));
  </code></pre>
  
  <p>I used <a href="https://stackoverflow.com/questions/12554186/how-to-serialize-deserialize-to-dictionaryint-string-from-custom-xml-not-us">this advice</a> to make the serializer. It works good, but I need to add to MyObject new member</p>
  
  <pre><code>List&lt;MyEnums.Enum2&gt; Codes = new List&lt;MyEnums.Enum2&gt;();
  </code></pre>
  
  <p>And store some values here</p>
  
  <pre><code>var a = new MyObject {...};
  a.Codes.Add(MyEnums.Enum2.Code1);
  a.Codes.Add(MyEnums.Enum2.Code2);
  MyObjectsDic.Add("Obj1", a);
  </code></pre>
  
  <p>But these list is serialized into file like</p>
  
  <pre><code>&lt;Codes&gt;Code1Code2&lt;Codes/&gt;
  </code></pre>
  
  <p>Without any space or delimiter. And I don't know how to make it more readable without modifications in serializer and without adding new odd code. The only idea I got is to keep already prepared string in MyObject instead of List&lt;...>. It isn't graceful, but simple and works. I don't read these data, just only write and save as log into file.<br>
  Or I need to change my so cool serializer?</p>
  
  <p><strong>Upd.</strong></p>
  
  <p>I used the solution below, but I'm receiving an <strong>exception</strong> on Windows XP. On other OS's it works good. I modified code to make it like helper not an class extension.  </p>
  
  <pre><code>Exception during dumping MyObjectsDic: There was an error reflecting type 'MyObject'.  
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
  at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
  at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
  at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
  at System.Xml.Serialization.XmlSerializer..ctor(Type type)
  at MyXmlSerializerHelper.SerializeToXElement[T](T obj, XmlSerializer serializer, Boolean omitStandardNamespaces) in MyXmlSerializerHelper.cs:line 16
  at MyXmlSerializerHelper.  &lt;SerializeToFile&gt;b__0[T](KeyValuePair'2 x) in MyXmlSerializerHelper.cs:line 5
  </code></pre>
  
  <p>The only idea I have - different versions of framework or some other religious issues on XP... Unfortunately, I can't install any other software or .Net version in production.</p>