ObjectGraphWithCycle Sample

Back to summary

Source

using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;

namespace XmlSerializersTest.Samples
{
    [Title("Object graph with cycle")]
    [SkipSerializer("Standard", "Object graph with cycle causes unrecoverable stack overflow")]
    [SkipSerializer("XAML", "Object graph with cycle causes unrecoverable stack overflow")]
    public class ObjectGraphWithCycle
    {
        public string Value { get; set; }

        [XmlIgnore]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<ObjectGraphWithCycle> Links { get; set; }

        public static ObjectGraphWithCycle GetSampleGraphWithCycle()
        {
            var node = new ObjectGraphWithCycle { Value = "root" };
            node.Links = new List<ObjectGraphWithCycle> {node};
            return node;
        }
    }
}

WCF-ref Serializer

<ObjectGraphWithCycle xmlns:i="http://www.w3.org/2001/XMLSchema-instance" z:Id="1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">
  <Links z:Id="2" z:Size="1">
    <ObjectGraphWithCycle z:Ref="1" i:nil="true" />
  </Links>
  <Value z:Id="3">root</Value>
</ObjectGraphWithCycle>