title: Dictionary Data Serialization in C#
body: <p>I want to convert my dictionary data into XML. I am trying to use XML serialization but I am getting an error. Below is my code.</p>
  
  <pre><code>class Program
  {
  
      static void Main(string[] args)
      {
          Dictionary&lt;int, AddressDetails&gt; objDic = new Dictionary&lt;int, AddressDetails&gt;();
          for (int i = 1; i &lt;= 5; i++)
          {
              AddressDetails obj = new AddressDetails();
              obj.HouseNo = i;
              obj.StreetName = "New Street One " + i;
              obj.City = "New Delhi One " + i;
              objDic.Add(i, obj);
          }
  
          string str = Utility.GetXMLFromObject(objDic);
      }
  
  }
  
  public class AddressDetails 
  {
      public int HouseNo { get; set; }
      public string StreetName { get; set; }
      public string City { get; set; }
      private string PoAddress { get; set; }
  }
  
  public static class Utility
  {
      public static string GetXMLFromObject(object o)
      {
          StringWriter sw = new StringWriter();
          XmlTextWriter tw = null;
          try
          {
              XmlSerializer serializer = new XmlSerializer(o.GetType());
              tw = new XmlTextWriter(sw);
              serializer.Serialize(tw, o);
          }
          catch (Exception ex)
          {
              //Handle Exception Code
          }
          finally
          {
              sw.Close();
              if (tw != null)
              {
                  tw.Close();
              }
          }
          return sw.ToString();
      }
  }
  </code></pre>
  
  <p>Error Code line : <code>XmlSerializer serializer = new XmlSerializer(o.GetType());</code></p>
  
  <p>Error Description : </p>
  
  <blockquote>
    <p>The type System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[ConsoleApplication5.AddressDetails, ConsoleApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] is not supported because it implements IDictionary.</p>
  </blockquote>
  
  <p>Please suggest.</p>