title: DEEPCLONE OF A SERIALIZED OBJECT
body: <p>I do have a .cs class file that represent a schema for a certain object.
  All the objects have a SerializableAttribute and I am able to serialize a model following the schema.</p>
  <pre class="lang-cs prettyprint-override"><code>    [System.CodeDom.Compiler.GeneratedCodeAttribute(&quot;System.Xml&quot;, &quot;4.8.4084.0&quot;)]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;)]
      [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = &quot;urn:strusoft&quot;, TypeName = &quot;databaseUser_defined_views&quot;)]
      [System.Xml.Serialization.XmlRootAttribute(ElementName = &quot;databaseUser_defined_views&quot;)]
      public partial class DatabaseUser_defined_views
      {
  
          private System.Collections.Generic.List&lt;View_type&gt; viewField;
  
          private string actual_viewField;
  
          private bool physical_viewField;
  
          private Displaymodes display_modeField;
  
          private System.Collections.Generic.List&lt;System.Xml.XmlAttribute&gt; anyAttrField;
  
          public DatabaseUser_defined_views()
          {
              this.physical_viewField = false;
              this.display_modeField = Displaymodes.Wireframe;
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlElementAttribute(&quot;view&quot;)]
          public System.Collections.Generic.List&lt;View_type&gt; View
          {
              get
              {
                  return this.viewField;
              }
              set
              {
                  this.viewField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlAttributeAttribute(AttributeName = &quot;actual_view&quot;)]
          public string Actual_view
          {
              get
              {
                  return this.actual_viewField;
              }
              set
              {
                  this.actual_viewField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlAttributeAttribute(AttributeName = &quot;physical_view&quot;)]
          [System.ComponentModel.DefaultValueAttribute(false)]
          public bool Physical_view
          {
              get
              {
                  return this.physical_viewField;
              }
              set
              {
                  this.physical_viewField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlAttributeAttribute(AttributeName = &quot;display_mode&quot;)]
          [System.ComponentModel.DefaultValueAttribute(Displaymodes.Wireframe)]
          public Displaymodes Display_mode
          {
              get
              {
                  return this.display_modeField;
              }
              set
              {
                  this.display_modeField = value;
              }
          }
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlAnyAttributeAttribute()]
          public System.Collections.Generic.List&lt;System.Xml.XmlAttribute&gt; AnyAttr
          {
              get
              {
                  return this.anyAttrField;
              }
              set
              {
                  this.anyAttrField = value;
              }
          }
      }
  </code></pre>
  <p>The issue happen when I try to DeepClone() some of those object.
  I am using the following function to DeepClone</p>
  <pre class="lang-cs prettyprint-override"><code>        public static T DeepClone&lt;T&gt;(this T obj)
          {
              using (var ms = new MemoryStream())
              {
                  var formatter = new BinaryFormatter();
                  formatter.Serialize(ms, obj);
                  ms.Position = 0;
  
                  return (T)formatter.Deserialize(ms);
              }
          }
  </code></pre>
  <p>The program throw the following exception which I do not fully understand:</p>
  <blockquote>
  <p>An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.Formatters.dll
  Type 'System.Xml.XmlElement' in Assembly 'System.Private.Xml, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' is not marked as serializable.&gt;</p>
  </blockquote>
  <p>Any suggestion?</p>