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

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