title: Serilializing Linq.Table to XML
body: <p>I have a very simple application which currently has a single Linq to Sql class based on a single table.</p>
  
  <p>I need to serialize (to XML) all rows in the table using the DataContext for the Linq To Sql class.</p>
  
  <p>How do I go about doing this?</p>
  
  <p>This is my current code :</p>
  
  <pre><code>    var db = new MyEntityDataContext();
      Stream fs = new FileStream("Output.xml", FileMode.Create);
      XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode);            
  
      serializer = new XmlSerializer(typeof(MyEntity));
      foreach (var row in db.MyEntitys)
      {
          // Serialize object to XML
          serializer.Serialize(writer,row);
      }
  
      writer.Close();
  </code></pre>
  
  <p>However it throws the following exception:
   <code>"Token StartElement in state Epilog would result in an invalid XML document."</code></p>
  
  <p>I have also tried:</p>
  
  <pre><code>XmlSerializer serializer2 = new XmlSerializer(db.MyEntitys.GetType());
  </code></pre>
  
  <p>but this throws a 
  <code>"To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy."</code> 
  exception.</p>