Posts

Showing posts from August, 2015

Object graph for type contains cycles and cannot be serialized

This issue occurs when parent and child classes has cyclic reference and when you tries to serialize it to json or xml. See the following code which gives the error.   [ DataContract ] public class Family {      [ DataMember ]      public IList < Parent > Parents;      [ DataMember ]      public IList < Child > Children; }   [ DataContract ] public class Parent {      [ DataMember ]      public string Name { get ; set ; }      [ DataMember ]      public IList < Child > Children { get ; set ; } }   [ DataContract ] public class Child {      [ DataMember ]      public string Name { get ; set ; }      [ DataMember ]      public Parent Father { get ; set ; }      [ DataMember ]      public Parent Mother { get ; set ; } } This is the sample object var dad = new Parent { Name = "John" };              var mum = new Parent { Name = "Mary" };                var kid1 =...