title: How do I serialize a Custom Collection in MongoDB.Bson?
body: <p>The documentation for the MongoDB.Bson.Serialization seems thin. I can't figure out how to get my custom collection into the MongoDB document correctly. I am using their <a href="http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-Representation" rel="nofollow">default c# Driver</a>.</p>
  
  <p>This is the class that I am trying to serialize, but when I look at the result after it is it serialized and then deserialized, the collection is empty. More specifically, it creates a new instance of the class, but the "_items" collection was coming out null. I found this unusual given that it was a private interior collection. This collection is a property of a much larger class, the other class properties are serializing correctly. I have been serializing this to XML using the built-in serializers and this worked fine. I figure it's probably something simple, but short of creating a custom serialization implementation I don't know what it is. Is there something I have to do to tell the serializer to treat this as a collection? </p>
  
  <p>Since I am am brand new to the MongoDB, I haven't figured out how to dump large raw documents to inspect what the MongoDB document itself looks like. That's what I am working on now.</p>
  
  <pre><code>[XmlRootAttribute(ElementName = "Children", IsNullable = true)] 
  public class ChildList : IList&lt;SurveyItem&gt;, ICollection&lt;SurveyItem&gt; 
  {
      private SurveyItem _parent = null;
      public ChildList(SurveyItem iParent)
      {
          _parent = iParent;      
      }
  
      private List&lt;SurveyItem&gt; items = new List&lt;SurveyItem&gt;();        
  
      #region ICollection&lt;SurveyItem&gt; Members
      public void Add(SurveyItem item)
      {
          //wire up the child.
          item.Parent = _parent;
          items.Add(item);
      }
  
      public void Clear()
      {
          items.Clear();
      }
  
      public bool Contains(SurveyItem item)
      {
          return items.Contains(item);
      }
  
      public void CopyTo(SurveyItem[] array, int arrayIndex)
      {
          items.CopyTo(array, arrayIndex);
      }
  
      public int Count
      {
          get { return items.Count; }
      }
  
      public bool IsReadOnly
      {
          get { return false; }
      }
  
      public bool Remove(SurveyItem item)
      {
          item.Parent = null;
          return items.Remove(item);
      }
      #endregion
  
      #region IEnumerable&lt;SurveyItem&gt; Members
      public IEnumerator&lt;SurveyItem&gt; GetEnumerator()
      {
          return items.GetEnumerator();
      }
      #endregion
  
      #region IEnumerable Members
      IEnumerator IEnumerable.GetEnumerator()
      {
          return (items as IEnumerable).GetEnumerator();
      }
      #endregion
  
      #region IList&lt;SurveyItem&gt; Members
      public int IndexOf(SurveyItem item)
      {
          return items.IndexOf(item);
      }
  
      public void Insert(int index, SurveyItem item)
      {
          item.Parent = _parent;
          items.Insert(index, item);
      }
  
      public void RemoveAt(int index)
      {
          items[index].Parent = null;
          items.RemoveAt(index);
      }
  
      public SurveyItem this[int index]
      {
          get
          {
              return items[index];
          }
          set
          {
              value.Parent = _parent;
              items[index] = value;
          }
      }
      #endregion
  
      public SurveyItem[] ToArray()
      {
          SurveyItem[] output = new SurveyItem[items.Count];
          for (int i = 0; i &lt; items.Count; i++)
          {
              output[i] = items[i];
          }
          return output;
      }
  }
  </code></pre>