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>

XAML Serializer

System.InvalidOperationException: Cannot serialize a generic type 'System.Collections.Generic.List`1[XmlSerializersTest.Samples.ObjectGraph]'.
   at System.Windows.Markup.Primitives.MarkupWriter.VerifyTypeIsSerializable(Type type)
   at System.Windows.Markup.Primitives.MarkupWriter.WriteItem(MarkupObject item, Scope scope)
   at System.Windows.Markup.Primitives.MarkupWriter.WriteItem(MarkupObject item, Scope scope)
   at System.Windows.Markup.Primitives.MarkupWriter.WriteItem(MarkupObject item)
   at System.Windows.Markup.Primitives.MarkupWriter.SaveAsXml(XmlWriter writer, MarkupObject item)
   at System.Windows.Markup.Primitives.MarkupWriter.SaveAsXml(XmlWriter writer, Object instance)
   at System.Windows.Markup.XamlWriter.Save(Object obj, XmlWriter xmlWriter)
   at XmlSerializersTest.Serializers.XamlSerializer.Serialize(Object obj, XmlWriter writer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\Serializers\XamlSerializer.cs:line 13
   at XmlSerializersTest.SampleProcessor.SerializeToString(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 162
   at XmlSerializersTest.SampleProcessor.RecordSerializationResults(Object obj, IXmlSerializer serializer) in C:\Ivan\dev\XmlSerializersTest\XmlSerializersTest\SampleProcessor.cs:line 115

Deserialization skipped

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>

WCF-ref Serializer

<ObjectGraph 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="2">
    <ObjectGraph z:Id="3">
      <Links z:Id="4" z:Size="1">
        <ObjectGraph z:Id="5">
          <Links i:nil="true" />
          <Value z:Id="6">Turkey</Value>
        </ObjectGraph>
      </Links>
      <Value z:Id="7">Europe</Value>
    </ObjectGraph>
    <ObjectGraph z:Id="8">
      <Links z:Id="9" z:Size="1">
        <ObjectGraph z:Ref="5" i:nil="true" />
      </Links>
      <Value z:Id="10">Asia</Value>
    </ObjectGraph>
  </Links>
  <Value z:Id="11">World</Value>
</ObjectGraph>