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;
        }
    }
}

WCF Serializer

<ObjectGraph xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XmlSerializersTest.Samples">
  <Links>
    <ObjectGraph>
      <Links>
        <ObjectGraph>
          <Links i:nil="true" />
          <Value>Turkey</Value>
        </ObjectGraph>
      </Links>
      <Value>Europe</Value>
    </ObjectGraph>
    <ObjectGraph>
      <Links>
        <ObjectGraph>
          <Links i:nil="true" />
          <Value>Turkey</Value>
        </ObjectGraph>
      </Links>
      <Value>Asia</Value>
    </ObjectGraph>
  </Links>
  <Value>World</Value>
</ObjectGraph>