title: XmlSerializer System.InvalidOperationException
body: <p>I have the following code in my application:</p>
  
  <pre><code>[Serializable]
  public class Class
  {
      private string name = "";
      private List&lt;Property&gt; property = new List&lt;Property&gt;();
      [XmlAttribute]
      public string Name
      {
          get { return name; }
          set { name = value; }
      }
      [XmlArray(ElementName = "Properties")]
      public List&lt;Property&gt; Property
      {
          get { return property; }
          set { property = value; }
      }
      public Class() { }
  }
  [Serializable]
  public class Property
  {
      private string name = "";
      private object value;
  
      [XmlAttribute]
      public string Name
      {
          get { return name; }
          set { name = value; }
      }
      [XmlElement]
      public object Value
      {
          get { return this.value; }
          set { this.value = value; }
      }
      public Property() { }
  }
  class Program
  {
      static void Main(string[] args)
      {
          List&lt;Class&gt; classList = GetClassList();
          SerializeConfig("test.xml", classList);
      }
      private static List&lt;Class&gt; GetClassList()
      {
          return new List&lt;Class&gt;(){
              new Class()
              {
                  Name = "MyFirstClass",
                  Property = new List&lt;Property&gt;() {
                      new Property(){ Name = "StringProperty",    Value = "Simple1"},
                      new Property(){ Name = "IntProperty",       Value = 3},
                      new Property(){ Name = "DoubleProperty",    Value = 5.5},
                      new Property(){ Name = "ListProperty",      Value = new List&lt;int&gt;(){
                          1,2,3,4,5
                          }
                      },
                  }
              }
          };
      }
      public static List&lt;Class&gt; DeserializeConfig(string configPath)
      {
          if (!File.Exists(configPath)) return null;
          try
          {
              XmlSerializer serializer = new XmlSerializer(typeof(List&lt;Class&gt;), new XmlRootAttribute("Classes"));
              using (FileStream fs = File.Open(configPath, FileMode.Open))
              {
                  return (List&lt;Class&gt;)serializer.Deserialize(fs);
              }
          }
          catch (Exception)
          {
              return null;
          }
      }
      public static bool SerializeConfig(string path, List&lt;Class&gt; config)
      {
          try
          {
              XmlSerializer serializer = new XmlSerializer(typeof(List&lt;Class&gt;), new XmlRootAttribute("Classes"));
              using (FileStream fs = File.Open(path, FileMode.Create))
              {
                  serializer.Serialize(fs, config);
              }
          }
          catch (Exception)
          {
              return false;
          }
          return true;
      }
  }
  </code></pre>
  
  <p>If you run this code, you will get a <code>System.InvalidOperationException</code> with the following message:</p>
  
  <pre><code>{System.InvalidOperationException: There was an error generating the XML document. ---&gt; System.InvalidOperationException: The type System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] may not be used in this context.
     at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write2_Property(String n, String ns, Property o, Boolean isNullable, Boolean needType)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write3_Class(String n, String ns, Class o, Boolean isNullable, Boolean needType)
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write4_Classes(Object o)
     --- End of inner exception stack trace ---
     at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
     at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces)
     at System.Xml.Serialization.XmlSerializer.Serialize(Stream stream, Object o)
     at SerializeTest.Program.SerializeConfig(String path, List`1 config) in [...]\Program.cs:line 102}
  </code></pre>
  
  <p>Why I can't serialize the List in this way? How I can build a workaround?</p>