|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Stencil Serialization in WPF Diagram | Syncfusion® |
| 4 | +description: Learn how to serialize and deserialize stencils in Syncfusion WPF Diagram (SfDiagram) control, including individual symbol groups. |
| 5 | +platform: wpf |
| 6 | +control: SfDiagram |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Stencil Serialization in WPF Diagram (SfDiagram) |
| 11 | + |
| 12 | +[Stencil](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Diagram.Stencil.html) serialization is the process of converting the state of stencil into a stream of bytes to recreate them when needed. Such streams can be stored in a database, as a file, or in memory. The reverse process is called deserialization. |
| 13 | + |
| 14 | +### Saving the Stencil |
| 15 | + |
| 16 | +In Stencil, [DataContractSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-8.0) is used for serialization. It allows you to serialize and save your stencil into a stream. Here is a simple code example showing how to save the stencil: |
| 17 | + |
| 18 | +{% tabs %} |
| 19 | +{% highlight C# %} |
| 20 | + |
| 21 | +// To Save as stream in file |
| 22 | +SaveFileDialog dialog = new SaveFileDialog(); |
| 23 | +dialog.Title = "Save XAML"; |
| 24 | +dialog.Filter = "XAML File (*.xaml)|*.xaml"; |
| 25 | +if (dialog.ShowDialog() == true) |
| 26 | +{ |
| 27 | + using (Stream str = File.Open(dialog.FileName, FileMode.OpenOrCreate)) |
| 28 | + { |
| 29 | + stencil.Save(str); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// To Save as memory stream |
| 34 | +MemoryStream str = new MemoryStream(); |
| 35 | +stencil.Save(str); |
| 36 | + |
| 37 | +{% endhighlight %} |
| 38 | +{% endtabs %} |
| 39 | + |
| 40 | +### Loading the Stencil |
| 41 | + |
| 42 | +On deserialization, the saved stream is used to load the Stencil. We can continue using previously saved stencil by loading the saved stream. Here is a simple code example showing how to load the stencil: |
| 43 | + |
| 44 | +{% tabs %} |
| 45 | +{% highlight C# %} |
| 46 | + |
| 47 | +// Load from saved XAML file |
| 48 | +OpenFileDialog dialog = new OpenFileDialog(); |
| 49 | +if (dialog.ShowDialog() == true) |
| 50 | +{ |
| 51 | + using (Stream myStream = dialog.OpenFile()) |
| 52 | + { |
| 53 | + stencil.Load(myStream); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Load from saved memory stream |
| 58 | +myStream.Position = 0; |
| 59 | +stencil.Load(myStream); |
| 60 | + |
| 61 | +{% endhighlight %} |
| 62 | +{% endtabs %} |
| 63 | + |
| 64 | +## Exporting and Importing Symbol Group in Stencil |
| 65 | + |
| 66 | +The Stencil also supports exporting and importing specific symbol groups. This functionality is useful when you need to save and reuse only certain symbol groups. |
| 67 | + |
| 68 | +### Exporting a Symbol Group |
| 69 | + |
| 70 | +In Stencil, the [ExportGroup](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Diagram.Stencil.Stencil.html#Syncfusion_UI_Xaml_Diagram_Stencil_Stencil_ExportGroup_System_IO_Stream_System_String___) method allows you to export SymbolGroups. By providing the symbol group names as parameters to this method, you can save the specified SymbolGroups to a stream, preserving all their properties for easy sharing or future use. Here is a simple code example showing how to export the symbol groups: |
| 71 | + |
| 72 | +{% tabs %} |
| 73 | +{% highlight C# %} |
| 74 | + |
| 75 | +string[] symbolGroupNames = { "Basic Shapes", "Flow Shapes", "BPMN Editor Shapes" }; |
| 76 | + |
| 77 | +SaveFileDialog dialog = new SaveFileDialog(); |
| 78 | +dialog.Title = "Save XAML"; |
| 79 | +dialog.Filter = "XAML File (*.xaml)|*.xaml"; |
| 80 | +if (dialog.ShowDialog() == true) |
| 81 | +{ |
| 82 | + using (Stream s = File.Open(dialog.FileName, FileMode.OpenOrCreate)) |
| 83 | + { |
| 84 | + stencil.ExportGroup(s, symbolGroupNames); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +{% endhighlight %} |
| 89 | +{% endtabs %} |
| 90 | + |
| 91 | +### Importing a Symbol Group |
| 92 | + |
| 93 | +To import the saved symbol groups back into the stencil, use the [ImportGroup](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Diagram.Stencil.Stencil.html#Syncfusion_UI_Xaml_Diagram_Stencil_Stencil_ImportGroup_System_IO_Stream_) method with the saved stream. This will load the stencil with the saved symbol groups. Here is a simple code example showing how to import the symbol groups: |
| 94 | + |
| 95 | +{% tabs %} |
| 96 | +{% highlight C# %} |
| 97 | + |
| 98 | +OpenFileDialog dialog = new OpenFileDialog(); |
| 99 | +if (dialog.ShowDialog() == true) |
| 100 | +{ |
| 101 | + using (Stream myStream = dialog.OpenFile()) |
| 102 | + { |
| 103 | + stencil.ImportGroup(myStream); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +{% endhighlight %} |
| 108 | +{% endtabs %} |
| 109 | + |
| 110 | + |
| 111 | +[View Sample in GitHub](https://github.com/SyncfusionExamples/WPF-Diagram-Examples/tree/master/Samples/Stencil/SymbolGroupSerialize) |
0 commit comments