title: How can I serialize an object with a Dictionary<string,object> property?
body: <p>In the example code below, I get this <strong>error</strong>:</p>
  
  <blockquote>
    <p>Element
    TestSerializeDictionary123.Customer.CustomProperties
    vom Typ
    System.Collections.Generic.Dictionary`2[[System.String,
    mscorlib, Version=2.0.0.0,
    Culture=neutral,
    PublicKeyToken=b77a5c561934e089],[System.Object,
    mscorlib, Version=2.0.0.0,
    Culture=neutral,
    PublicKeyToken=b77a5c561934e089]] can
    not be serialized because it
    implements IDictionary.</p>
  </blockquote>
  
  <p>When I take out the Dictionary property, it works <strong>fine</strong>.</p>
  
  <p><strong>How can I serialize this Customer object with the dictionary property? Or what replacement type for Dictionary can I use that would be serializable?</strong> </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Xml.Serialization;
  using System.IO;
  using System.Xml;
  using System.Text;
  
  namespace TestSerializeDictionary123
  {
      public class Program
      {
          static void Main(string[] args)
          {
              List&lt;Customer&gt; customers = Customer.GetCustomers();
  
              Console.WriteLine("--- Serializing ------------------");
  
              foreach (var customer in customers)
              {
                  Console.WriteLine("Serializing " + customer.GetFullName() + "...");
                  string xml = XmlHelpers.SerializeObject&lt;Customer&gt;(customer);
                  Console.WriteLine(xml);
                  Console.WriteLine("Deserializing ...");
                  Customer customer2 = XmlHelpers.DeserializeObject&lt;Customer&gt;(xml);
                  Console.WriteLine(customer2.GetFullName());
                  Console.WriteLine("---");
              }
  
              Console.ReadLine();
          }
      }
  
      public static class StringHelpers
      {
          public static String UTF8ByteArrayToString(Byte[] characters)
          {
              UTF8Encoding encoding = new UTF8Encoding();
              String constructedString = encoding.GetString(characters);
              return (constructedString);
          }
  
          public static Byte[] StringToUTF8ByteArray(String pXmlString)
          {
              UTF8Encoding encoding = new UTF8Encoding();
              Byte[] byteArray = encoding.GetBytes(pXmlString);
              return byteArray;
          }
      }
  
      public static class XmlHelpers
      {
          public static string SerializeObject&lt;T&gt;(object o)
          {
              MemoryStream ms = new MemoryStream();
              XmlSerializer xs = new XmlSerializer(typeof(T));
              XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
              xs.Serialize(xtw, o);
              ms = (MemoryStream)xtw.BaseStream;
              return StringHelpers.UTF8ByteArrayToString(ms.ToArray());
          }
  
          public static T DeserializeObject&lt;T&gt;(string xml)
          {
              XmlSerializer xs = new XmlSerializer(typeof(T));
              MemoryStream ms = new MemoryStream(StringHelpers.StringToUTF8ByteArray(xml));
              XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
              return (T)xs.Deserialize(ms);
          }
      }
  
      public class Customer
      {
          public int Id { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string Street { get; set; }
          public string Location { get; set; }
          public string ZipCode { get; set; }
          public Dictionary&lt;string,object&gt; CustomProperties { get; set; }
  
          private int internalValue = 23;
  
          public static List&lt;Customer&gt; GetCustomers()
          {
              List&lt;Customer&gt; customers = new List&lt;Customer&gt;();
              customers.Add(new Customer { Id = 1, FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
              customers.Add(new Customer { Id = 2, FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
              customers.Add(new Customer { Id = 3, FirstName = "Jack", LastName = "Johnson", ZipCode = "23111" });
              customers.Add(new Customer { Id = 4, FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
              customers.Add(new Customer { Id = 5, FirstName = "Henry", LastName = "Anderson", ZipCode = "16623" });
              return customers;
          }
  
          public string GetFullName()
          {
              return FirstName + " " + LastName + "(" + internalValue + ")";
          }
  
      }
  }
  </code></pre>