title: How to write in a Xml using derived classes?
body: <p>maybe the title is not the best description.</p>
  
  <p>My situation is, depending on a interface I want to write one of its properties which is object. Please watch the following example:</p>
  
  <pre><code>public interface IFoo
  {
      // anothers properties...
      object Value { get; }
  }
  </code></pre>
  
  <p>And I have three implementations of it here, check that Value can be is hidden the true datatype.</p>
  
  <pre><code>public class FooA : IFoo
  {
      public string Value { get; set; }
      object IFoo.Value { get { return Value; } }
  }
  
  public class FooB : IFoo
  {
      public int Value { get; set; }
      object IFoo.Value { get { return Value; } }
  }
  
  public class FooC : IFoo
  {
      public List&lt;double&gt; Value { get; set; }
      object IFoo.Value { get { return Value; } }
  }
  </code></pre>
  
  <p>In my scenario was very important not using generics. So with this implementation I need to write in a Xml just the property Value.</p>
  
  <p>For example:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          List&lt;IFoo&gt; fooList = new List&lt;IFoo&gt;()
          {
              new FooA() { Value = "String" },
              new FooB() { Value = 2 },
              new FooC() { Value = new List&lt;double&gt;() {2, 3.4 } } 
          };
  
          // Write in a Xml the elements of this list with the property Value (and the problem is the datartype,
          // strings, ints, lists, etc)
          // ...
      }
  </code></pre>
  
  <p>I was thinking using XmlSerialization, but I don't want to store all the properties of the object, just Value property.</p>
  
  <p>I need a way to write the property Value (not to store all the properties of that interface) and a Way to know which implementation of IFoo I'm using.</p>
  
  <p>UPDATE: I wanna have an XML file like this.</p>
  
  <pre><code>&lt;Foos&gt;
      &lt;Foo xsi:Type="FooA" Value="String" /&gt;
      &lt;Foo xsi:Type="FooB" Value="2" /&gt;
      &lt;Foo xsi:Type="FooC" &gt;
          &lt;Value&gt;
              &lt;List xsi:Type="Double"&gt;
                  &lt;Element&gt;2&lt;/Element&gt;
                  &lt;Element&gt;3.4&lt;/Element&gt;
              &lt;/List&gt;
          &lt;/Value&gt;
      &lt;/Foo
  &lt;/Foos&gt;
  </code></pre>
  
  <p>This is just an idea. The idea is to store just a property of IFoo and the capability to get the concrete object with the value.</p>
  
  <p>UPDATE:</p>
  
  <p>Thanks to Raj Nagalingam to help me with the idea. This is what I've accomplished.</p>
  
  <pre><code>public interface IFoo
  {
      object Value { get; }
  }
  
  public abstract class Foo&lt;T&gt; : IFoo, IXmlSerializable
  {
      [XmlElement]
      public T Value { get; set; }
      [XmlIgnore]
      object IFoo.Value { get { return Value; } }
  
      XmlSchema IXmlSerializable.GetSchema() { return null; }
      void IXmlSerializable.ReadXml(XmlReader reader) { throw new NotImplementedException(); }
      void IXmlSerializable.WriteXml(XmlWriter writer)
      {
          XmlSerializer serial = new XmlSerializer(Value.GetType());
          serial.Serialize(writer, Value);
      }
  }
  
  public class FooA : Foo&lt;string&gt; { }
  public class FooB : Foo&lt;int&gt; { }
  public class FooC : Foo&lt;List&lt;Double&gt;&gt; { }
  public class FooContainer : List&lt;IFoo&gt;, IXmlSerializable
  {
      public XmlSchema GetSchema() { return null; }
      public void ReadXml(XmlReader reader) { throw new NotImplementedException(); } 
      public void WriteXml(XmlWriter writer)
      {
          ForEach(x =&gt; 
              {
                  XmlSerializer serial = new XmlSerializer(x.GetType());
                  serial.Serialize(writer, x);
              });
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          FooContainer fooList = new FooContainer()
          {
              new FooA() { Value = "String" },
              new FooB() { Value = 2 },
              new FooC() { Value = new List&lt;double&gt;() {2, 3.4 } } 
          };
  
          XmlSerializer serializer = new XmlSerializer(fooList.GetType(),
              new Type[] { typeof(FooA), typeof(FooB), typeof(FooC) });
          System.IO.TextWriter textWriter = new System.IO.StreamWriter(@"C:\temp\demo.xml");
          serializer.Serialize(textWriter, fooList);
          textWriter.Close();
      }
  }
  </code></pre>
  
  <p>And it works, but I guess it's impossible to recover the list with the concrete classes, Using the standard serialization I see that it's written using xsi, and this time I can't see them. How can I recover my objects?</p>