title: C# Castle ActiveRecord: How to elegantly (XML) serialize ActiveRecord objects?
body: <p>I'm having a difficult time finding information on how to elegantly serialize ActiveRecord objects.</p>
  
  <p>We would like to use XML as the format because we need to output our objects in such a way that another program will be able to feasibly parse them.</p>
  
  <p>XML-Serialization is usually very easy and straightforward to implement, but the problem comes when trying to serialize an object returned from the ActiveRecord database.  The database returns a proxy class of the object, the type of which cannot be explicitly anticipated via the <code>[XmlInclude]</code> attribute.</p>
  
  <p>For example:</p>
  
  <pre><code>public class Foo : ActiveRecordLinqBase&lt;Foo&gt;
  {
     public virtual string Bar{get;set;}
  
     public virtual int FooId{get;set;}
  
     public Foo(string bar)
     {
        Bar = bar;
     }
  
     public static void FooSerializeExample()
     {
        Foo tehFoozor = new Foo("omgFoo!");
        tehFoozor.SaveAndFlush();
        int id = tehFoozor.FooId;
  
        //...
        //Assume new ActiveRecord session.
  
        XmlSerializer serializer = new XmlSerializer(typeof(Foo));
  
        Foo tehFoozorToSerialize = Foo.Find(id);
  
        using(Stream stream = File.OpenWrite("tehFoozor.xml"))
        {
           serializer.Serialize(stream, tehFoozorToSerialize); //Will fail
        }
     }
  }
  </code></pre>
  
  <p>When serializing here, we'll get a message:<br>
  "The type FooProxy2e2de24df9be42909d13a67fdb00b981 was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."</p>
  
  <p>where the Proxy type will be completely unpredictable (at least to my knowledge).</p>
  
  <p>As a temporary solution, my team has stubbed out each AR object's properties into interfaces.  Then, we implemented "Container" objects for each, which are essentially Xml-Serializable non-AR versions of the objects.  Considering the fact that we currently have 18 different AR objects which are serialized, that's 36 additional files in our solution!  Something (everything) tells me this is a bad solution, but I've not been able to find a better way.</p>
  
  <p>We also tried using the Soap Formatter, but since ActiveRecrodLinqBase&lt;> isn't "marked as serializable", this was a dead end also.</p>