This is a migrated thread and some comments may be shown as answers.

Serialize/Deserialize collections in SerializableGraphSourceBase

2 Answers 197 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Christos
Top achievements
Rank 1
Christos asked on 24 Jul 2015, 05:25 PM

Following your Serialize a Databound Diagram article
I was wondering whether the RadDiagram Serialization/Deserialization system supports serialization of a collection within the GraphSource Nodes. In particular I am interested on using an ObservableCollection:

01.public class OrgItem : HierarchicalNodeViewModel
02.{
03.    public OrgItem()
04.    {
05.       this.MyCollection = new ObservableCollection<string>();
06.    }
07.    public ObservableCollection<string> MyCollection
08.    {
09.        get;
10.    }
11.
12.
13.
14.}
15.private void BindGraphSource()
16.{
17.    int uniqueIdCounter = 0;
18.    GraphSource source = new GraphSource();
19.    OrgItem rootItem = new OrgItem()
20.    {
21.        Title = "CEO",
22.        Position = new Point(200, 20),
23.        Id = (uniqueIdCounter++).ToString()
24.    };
25.    rootItem.MyCollection.Add(new MyClass(“Test1”));
26.    rootItem.MyCollection.Add(new MyClass(“Test2”));
27.    source.AddNode(rootItem);
28.
29.
30.
31.}
32.public class MyClass
33.{
34.    public MyClass(){};
35.    public MyClass(string myValue)
36.    {
37.        this.MyValue = myValue;
38.    }
39.    public string MyValue { get; set; }
40.}

 

Could I possibly Serialize/Deserialize the ObservableCollection in the OrgItem?

If that is possible how should i serialize/deserialize the collection in the GraphSource?

01.public class GraphSource : SerializableGraphSourceBase<OrgItem, OrgLink>
02.{
03.    public override string GetNodeUniqueId(OrgItem node)
04.    {
05.        return node.Id;
06.    }
07.    public override void SerializeNode(OrgItem node,
08.        Telerik.Windows.Diagrams.Core.SerializationInfo info)
09.    {
10.        base.SerializeNode(node, info);
11.        info["Title"] = node.Title;
12.    }
13.    public override OrgItem DeserializeNode(
14.        Telerik.Windows.Diagrams.Core.IShape shape,
15.        Telerik.Windows.Diagrams.Core.SerializationInfo info)
16.    {
17.        base.DeserializeNode(shape, info);
18.        if (info["Title"] != null)
19.        {
20.            return new OrgItem(info["Title"].ToString());
21.        }
22.        return null;
23.    }
24.}

 

Many thanks in advance,

Christos

2 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 28 Jul 2015, 09:48 AM
Hello Christos,

The RadDiagram's serialization service doesn't support saving complex objects. In order to save such you will need to serialize it with custom code. For example, you can save the properties of the object in the SerializationInfo and then use them in the deserialization process to create a new object. Or you can use the native XmlSerializer class to serialize the object (or collection) in xml string and then deserialize it.

For your convenience I prepared a sample project demonstrating the approach with the XmlSerializer. Please give it a try and let me know if it works for you.

Regards,
Martin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Christos
Top achievements
Rank 1
answered on 01 Aug 2015, 01:59 PM

Hello Martin,

 

Thank you for your reply, I used DataContractSerializer and it worked like a charm :)

Tags
Diagram
Asked by
Christos
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Christos
Top achievements
Rank 1
Share this question
or