Serialize/Deserialize the ControlNode
In WinForms Diagram Control, we don’t have a support to serialize the hosted control in Syncfusion.Windows.Forms.Diagram.ControlNode when the hosted control is non-serialized control. However, we can customize the Diagram’s Syncfusion.Windows.Forms.Diagram.ControlNode class and override the SerializeNode() as well as DeserializeNode() methods to achieve the requirement. The overridden methods contain HashTable collection as a parameter and it is used to add the Hosted control’s needed objects (i.e. serializable objects alone) into that collection while serializing/deserializing the customized control node.
The following steps contains how to serializing/deserializing the hosted control if the hosted control is non-serializable object.
Step1:
The following code example shows creating the custom controlNode
[C#]
[Serializable] public class CustomCtrlNode : ControlNode { public CustomCtrlNode(Control ctrlHosting, RectangleF rectBounds) : base(ctrlHosting, rectBounds) { } public CustomCtrlNode(CustomCtrlNode src) : base(src) { } protected CustomCtrlNode(SerializationInfo info, StreamingContext context) : base(info, context) { } protected override void GetObjectData(SerializationInfo info, StreamingContext context) { base.GetObjectData(info, context); } /// <summary> /// Override to serialize the serializable objects /// </summary> /// <param name="hashProperties">hashtable collection</param> public override void SerializeNode(Hashtable hashProperties) { base.SerializeNode(hashProperties); //Add your logic codes here... } /// <summary> /// Override to deserialize the serializable objects /// </summary> /// <param name="hashProperties">hashtable collection</param> public override void DeSerializeNode(Hashtable hashProps) { base.DeSerializeNode(hashProps); //Add your logic codes here... } public override object Clone() { return new CustomCtrlNode(this); } }
[VB]
<Serializable> Public Class CustomCtrlNode Inherits ControlNode Public Sub New(ByVal ctrlHosting As Control, ByVal rectBounds As RectangleF) MyBase.New(ctrlHosting, rectBounds) End Sub Public Sub New(ByVal src As CustomCtrlNode) MyBase.New(src) End Sub Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext) MyBase.New(info, context) End Sub Protected Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) MyBase.GetObjectData(info, context) End Sub ''' <summary> ''' Override to serialize the serializable objects ''' </summary> ''' <param name="hashProperties">hashtable collection</param> Public Overrides Sub SerializeNode(ByVal hashProperties As Hashtable) MyBase.SerializeNode(hashProperties) 'Add your logic codes here... End Sub ''' <summary> ''' Override to deserialize the serializable objects ''' </summary> ''' <param name="hashProperties">hashtable collection</param> Public Overrides Sub DeSerializeNode(ByVal hashProps As Hashtable) MyBase.DeSerializeNode(hashProps) 'Add your logic codes here... End Sub Public Overrides Function Clone() As Object Return New CustomCtrlNode(Me) End Function End Class
Step 2:
While Deserializing, we need to initialize the hosted control and reassign the deserialized objects into the initialized control’s objects.
The following code example shows how to deserializing the hosted control’s objects and reassign to the hosted control.
[C#]
public override void SerializeNode(Hashtable hashProperties) { base.SerializeNode(hashProperties); //here diagram as a Hosted control Diagram diagram = HostingControl as Diagram; Hashtable hashtable = new Hashtable(); //Serializing the Diagram’s Model and View properties (Needed properties) hashtable.Add("Model", diagram.Model); hashtable.Add("View", diagram.View); byte[] compressed; BinaryFormatter binaryFormatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(); memoryStream.Position = 0; binaryFormatter.Serialize(memoryStream, hashtable); compressed = memoryStream.ToArray(); //To adding the hashtable hashProperties.Add("propValues", compressed); }
[VB]
Public Overrides Sub SerializeNode(ByVal hashProperties As Hashtable) MyBase.SerializeNode(hashProperties) 'here diagram as a Hosted control Dim diagram As Diagram = TryCast(HostingControl, Diagram) Dim hashtable As New Hashtable() 'Serializing the Diagram’s Model and View properties (Needed properties) hashtable.Add("Model", diagram.Model) hashtable.Add("View", diagram.View) Dim compressed() As Byte Dim binaryFormatter As New BinaryFormatter() Dim memoryStream As New MemoryStream() memoryStream.Position = 0 binaryFormatter.Serialize(memoryStream, hashtable) compressed = memoryStream.ToArray() 'To adding the hashtable hashProperties.Add("propValues", compressed) End Sub
Step 3:
While deserializing, we need to initialize the hosted control and reassign the serialized objects into the initialized hosted control.
The following code example shows how to deserializing the hosted control’s objects.
[C#]
public override void DeSerializeNode(Hashtable hashProps) { base.DeSerializeNode(hashProps); Hashtable hashtable = new Hashtable(); //Desrializing the Diagram Model and View properties object entry = hashProps["propValues"]; if (entry is byte[]) { byte[] compressed = entry as byte[]; MemoryStream decompMS = new MemoryStream(compressed as byte[]); BinaryFormatter bf = new BinaryFormatter(); decompMS.Position = 0; hashtable = (Hashtable)bf.Deserialize(decompMS); } else hashtable = (Hashtable)entry; //intializing the Diagram control Diagram dgm = new Diagram(); //Getting the Model and view properties from Deserialized object (Needed proeperties) Model model = hashtable["Model"] as Model; Syncfusion.Windows.Forms.Diagram.View view = hashtable["View"] as Syncfusion.Windows.Forms.Diagram.View; DiagramDocument doc = new DiagramDocument(model, view); dgm.Document = doc; //to assign the hosted control m_ctrlHosting = dgm; }
[VB]
Public Overrides Sub DeSerializeNode(ByVal hashProps As Hashtable) MyBase.DeSerializeNode(hashProps) Dim hashtable As New Hashtable() 'Desrializing the Diagram Model and View properties Dim entry As Object = hashProps("propValues") If TypeOf entry Is Byte() Then Dim compressed() As Byte = TryCast(entry, Byte()) Dim decompMS As New MemoryStream(TryCast(compressed, Byte())) Dim bf As New BinaryFormatter() decompMS.Position = 0 hashtable = CType(bf.Deserialize(decompMS), Hashtable) Else hashtable = CType(entry, Hashtable) End If 'intializing the Diagram control Dim dgm As New Diagram() 'Getting the Model and view properties from Deserialized object (Needed proeperties) Dim model As Model = TryCast(hashtable("Model"), Model) Dim view As Syncfusion.Windows.Forms.Diagram.View = TryCast(hashtable("View"), Syncfusion.Windows.Forms.Diagram.View) Dim doc As New DiagramDocument(model, view) dgm.Document = doc 'to assign the hosted control m_ctrlHosting = dgm End Sub
Sample: