|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Diagram Serialization in Windows Forms Diagram control | Syncfusion |
| 4 | +description: Learn about Diagram Serialization support in Syncfusion Windows Forms Diagram control and more details. |
| 5 | +platform: windowsforms |
| 6 | +control: Diagram |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Serialization in Windows Forms Diagram |
| 11 | + |
| 12 | +Serialization is the process of converting the state of diagram objects into a byte stream, allowing them to be recreated later. These streams can be stored in a database, a file, or memory. The reverse process is known as deserialization. |
| 13 | + |
| 14 | +## Save |
| 15 | + |
| 16 | +In the diagram, [DataContractSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-8.0) is used for serialization. The features provided by `DataContractSerializer` apply to diagram serialization, supporting saving the diagram to a stream while preserving all its properties. |
| 17 | + |
| 18 | +### Saving a Diagram via Filename |
| 19 | + |
| 20 | +{% tabs %} |
| 21 | +{% highlight c# %} |
| 22 | + |
| 23 | +SaveFileDialog saveFileDialog = new SaveFileDialog(); |
| 24 | +saveFileDialog.Filter = @"XML Diagram (*.xml)|*.xml|All files (*.*)|*.*"; |
| 25 | +if (saveFileDialog.ShowDialog(this) == DialogResult.OK) |
| 26 | +{ |
| 27 | + // Save the diagram to a file path |
| 28 | + diagram1.SaveXml(this.saveFileDialog.FileName); |
| 29 | + |
| 30 | + this.diagram1.Refresh(); |
| 31 | +} |
| 32 | + |
| 33 | +{% endhighlight %} |
| 34 | +{% endtabs %} |
| 35 | + |
| 36 | +### Saving a Diagram via Stream |
| 37 | + |
| 38 | +{% tabs %} |
| 39 | +{% highlight c# %} |
| 40 | + |
| 41 | +SaveFileDialog saveFileDialog = new SaveFileDialog(); |
| 42 | +saveFileDialog.Filter = @"XML Diagram (*.xml)|*.xml|All files (*.*)|*.*"; |
| 43 | +if (saveFileDialog.ShowDialog(this) == DialogResult.OK) |
| 44 | +{ |
| 45 | + // Save the diagram to a file stream |
| 46 | + using (Stream stream = File.Open(saveFileDialog.FileName, FileMode.CreateNew)) |
| 47 | + { |
| 48 | + diagram1.SaveXml(stream); |
| 49 | + } |
| 50 | + |
| 51 | + this.diagram1.Refresh(); |
| 52 | +} |
| 53 | + |
| 54 | +{% endhighlight %} |
| 55 | +{% endtabs %} |
| 56 | + |
| 57 | +## Load |
| 58 | + |
| 59 | +Deserialization uses the saved stream to load the diagram's nodes and connectors into the current view. This allows you to continue working on the previously saved diagram by loading the appropriate stream. |
| 60 | + |
| 61 | +### Loading a Diagram from a File Path |
| 62 | + |
| 63 | +{% tabs %} |
| 64 | +{% highlight c# %} |
| 65 | + |
| 66 | +OpenFileDialog openFileDialog = new OpenFileDialog(); |
| 67 | +openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*"; |
| 68 | +openFileDialog.DefaultExt = "*.xml"; |
| 69 | +openFileDialog.Title = "Open Diagram"; |
| 70 | +if (openFileDialog.ShowDialog(this) == DialogResult.OK) |
| 71 | +{ |
| 72 | + string filePath = openFileDialog.FileName; |
| 73 | + |
| 74 | + // Load the diagram from the file path |
| 75 | + diagram1.LoadXml(filePath); |
| 76 | + |
| 77 | + this.diagram1.Refresh(); |
| 78 | +} |
| 79 | + |
| 80 | +{% endhighlight %} |
| 81 | +{% endtabs %} |
| 82 | + |
| 83 | +### Loading a Diagram from a Stream |
| 84 | + |
| 85 | +{% tabs %} |
| 86 | +{% highlight c# %} |
| 87 | + |
| 88 | +OpenFileDialog openFileDialog = new OpenFileDialog(); |
| 89 | +openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*"; |
| 90 | +openFileDialog.DefaultExt = "*.xml"; |
| 91 | +openFileDialog.Title = "Open Diagram"; |
| 92 | +if (openFileDialog.ShowDialog(this) == DialogResult.OK) |
| 93 | +{ |
| 94 | + string filePath = openFileDialog.FileName; |
| 95 | + |
| 96 | + // Load the diagram from a file stream |
| 97 | + using (Stream myStream = openFileDialog.OpenFile()) |
| 98 | + { |
| 99 | + diagram1.LoadXml(myStream); |
| 100 | + } |
| 101 | + |
| 102 | + this.diagram1.Refresh(); |
| 103 | +} |
| 104 | + |
| 105 | +{% endhighlight %} |
| 106 | +{% endtabs %} |
| 107 | + |
| 108 | +### Loading a Diagram from a File with Exception Handling |
| 109 | + |
| 110 | +{% tabs %} |
| 111 | +{% highlight c# %} |
| 112 | + |
| 113 | +OpenFileDialog openFileDialog = new OpenFileDialog(); |
| 114 | +openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*"; |
| 115 | +openFileDialog.DefaultExt = "*.xml"; |
| 116 | +openFileDialog.Title = "Open Diagram"; |
| 117 | +if (openFileDialog.ShowDialog(this) == DialogResult.OK) |
| 118 | +{ |
| 119 | + string filePath = openFileDialog.FileName; |
| 120 | + |
| 121 | + // Load the diagram from the file path with exception handling |
| 122 | + diagram1.LoadXml(filePath, out Exception exception); |
| 123 | + |
| 124 | + this.diagram1.Refresh(); |
| 125 | +} |
| 126 | + |
| 127 | +{% endhighlight %} |
| 128 | +{% endtabs %} |
| 129 | + |
| 130 | +### Loading a Diagram from a Stream with Exception Handling |
| 131 | + |
| 132 | +{% tabs %} |
| 133 | +{% highlight c# %} |
| 134 | + |
| 135 | +OpenFileDialog openFileDialog = new OpenFileDialog(); |
| 136 | +openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*"; |
| 137 | +openFileDialog.DefaultExt = "*.xml"; |
| 138 | +openFileDialog.Title = "Open Diagram"; |
| 139 | +if (openFileDialog.ShowDialog(this) == DialogResult.OK) |
| 140 | +{ |
| 141 | + string filePath = openFileDialog.FileName; |
| 142 | + |
| 143 | + // Load the diagram from the stream with exception handling |
| 144 | + using (Stream myStream = openFileDialog.OpenFile()) |
| 145 | + { |
| 146 | + diagram1.LoadXml(myStream, out Exception ex); |
| 147 | + } |
| 148 | + |
| 149 | + this.diagram1.Refresh(); |
| 150 | +} |
| 151 | + |
| 152 | +{% endhighlight %} |
| 153 | +{% endtabs %} |
| 154 | + |
| 155 | +## Checking if the Diagram's Model is Modified |
| 156 | + |
| 157 | +The [Modified](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Windows.Forms.Diagram.Model.html#Syncfusion_Windows_Forms_Diagram_Model_Modified) property of the Diagram's [Model](https://help.syncfusion.com/cr/windowsforms/Syncfusion.Windows.Forms.Diagram.Model.html) is a boolean value indicating whether the diagram has any unsaved changes. It tracks all changes made to the diagram, including node additions and other modifications to the diagram elements. |
| 158 | + |
| 159 | +{% tabs %} |
| 160 | +{% highlight c# %} |
| 161 | +if (diagram1 != null && diagram1.Model.Modified) |
| 162 | +{ |
| 163 | + MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show( |
| 164 | + "Do you want to save the Diagram?", |
| 165 | + "SfDiagram", |
| 166 | + MessageBoxButton.YesNo); |
| 167 | + if (messageBoxResult == MessageBoxResult.Yes) |
| 168 | + { |
| 169 | + SaveDiagram(); |
| 170 | + } |
| 171 | +} |
| 172 | +{% endhighlight %} |
| 173 | +{% endtabs %} |
0 commit comments