title: DataContract Parent reference in child property - how to avoid infinite serialization loop
body: <p>In a WCF service, I have a situation where there is a datacontract (object) that contains a collection of child items (where the collection has a datamemeber attribute). The objects in the child collection have a reference to the parent as a property. If the parent object child collection is empty or contains a null parent reference, all is good, but if the parent reference is populated and parent children are populated, it serializes for ever. </p>
  
  <p>Here's a set of test console code to show you what I'm talking about.</p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
  
              MyParentObject parent = BuildParent();
  
              string xml = Serializer&lt;MyParentObject&gt;.Serialize(parent);
  
              System.Console.Write(xml);
  
              System.Console.ReadKey();
  
          }
  
          static MyParentObject BuildParent()
          {
  
              MyParentObject parent = new MyParentObject();
              parent.MyParentObjectId = 123;
              parent.MyParentObjectName = "Test Parent Object";
              parent.MyChildren = new List&lt;MyChildObject&gt;();
  
              for (int i = 0; i &lt; 10; i++)
              {
                  MyChildObject child = new MyChildObject();
                  child.MyParent = parent;
                  child.MyChildObjectId = i;
                  child.MyChildObjectName = string.Format("Test Child Name {0}", i.ToString());
                  parent.MyChildren.Add(child);
              }
  
              return parent;
  
          }
  
      }
  
  
      [DataContract]
      public class MyParentObject
      {
  
          [DataMember]
          public int MyParentObjectId { get; set; }
  
          [DataMember]
          public string MyParentObjectName { get; set; }
  
          [DataMember]
          public List&lt;MyChildObject&gt; MyChildren { get; set; }
  
      }
  
      [DataContract]
      public class MyChildObject
      {
          [DataMember]
          public MyParentObject MyParent { get; set; }
  
          [DataMember]
          public int MyChildObjectId { get; set; }
  
          [DataMember]
          public string MyChildObjectName { get; set; }
  
      }
  
  public class Serializer&lt;T&gt;
      {
  
          public static string Serialize(T entity)
          {
  
              StringBuilder sb = new StringBuilder();
  
  
              DataContractSerializer dcs = new DataContractSerializer(typeof(T));
  
              using (XmlWriter writer = XmlWriter.Create(sb))
              {
  
                  dcs.WriteObject(writer, entity);
                  writer.Flush();
              }
  
              return sb.ToString();
          }
  
      }
  </code></pre>
  
  <p>Other than clearing out child/parent references that cause this infinite serialization loop, are there any ways around this?</p>
  
  <p>EDIT: I know I can just remove the datamember attribute, but I'd like to keep it, and simply not serialize infinitely.</p>