ObjectGraph Sample

Back to summary

Source

using System.Collections.Generic;

namespace XmlSerializersTest.Samples
{
    [Title("Object graph")]
    public class ObjectGraph
    {
        public string Value { get; set; }
        public List<ObjectGraph> Links { get; set; }

        public static ObjectGraph GetSampleGraph()
        {
            var world = new ObjectGraph { Value = "World" };
            var europe = new ObjectGraph { Value = "Europe" };
            var asia = new ObjectGraph { Value = "Asia" };
            var turkey = new ObjectGraph { Value = "Turkey" };

            world.Links = new List<ObjectGraph> { europe, asia };
            europe.Links = new List<ObjectGraph> { turkey };
            asia.Links = new List<ObjectGraph> { turkey };
            return world;
        }
    }
}

Standard Serializer

<ObjectGraph>
  <Value>World</Value>
  <Links>
    <ObjectGraph>
      <Value>Europe</Value>
      <Links>
        <ObjectGraph>
          <Value>Turkey</Value>
        </ObjectGraph>
      </Links>
    </ObjectGraph>
    <ObjectGraph>
      <Value>Asia</Value>
      <Links>
        <ObjectGraph>
          <Value>Turkey</Value>
        </ObjectGraph>
      </Links>
    </ObjectGraph>
  </Links>
</ObjectGraph>