Skip to content

WF-905069: UG Update For DataContractSerializer Support #1045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions WindowsForms/Diagram/Serialization/Diagram-Serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
---
layout: post
title: Diagram Serialization in Windows Forms Diagram control | Syncfusion
description: Learn about Diagram Serialization support in Syncfusion Windows Forms Diagram control and more details.
platform: windowsforms
control: Diagram
documentation: ug
---

# Serialization in Windows Forms Diagram

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.

## Save

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.

### Saving a Diagram via Filename

{% tabs %}
{% highlight c# %}

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = @"XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
// Save the diagram to a file path
diagram1.SaveXml(this.saveFileDialog.FileName);

this.diagram1.Refresh();
}

{% endhighlight %}
{% endtabs %}

### Saving a Diagram via Stream

{% tabs %}
{% highlight c# %}

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = @"XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
// Save the diagram to a file stream
using (Stream stream = File.Open(saveFileDialog.FileName, FileMode.CreateNew))
{
diagram1.SaveXml(stream);
}

this.diagram1.Refresh();
}

{% endhighlight %}
{% endtabs %}

## Load

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.

### Loading a Diagram from a File Path

{% tabs %}
{% highlight c# %}

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
openFileDialog.DefaultExt = "*.xml";
openFileDialog.Title = "Open Diagram";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string filePath = openFileDialog.FileName;

// Load the diagram from the file path
diagram1.LoadXml(filePath);

this.diagram1.Refresh();
}

{% endhighlight %}
{% endtabs %}

### Loading a Diagram from a Stream

{% tabs %}
{% highlight c# %}

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
openFileDialog.DefaultExt = "*.xml";
openFileDialog.Title = "Open Diagram";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string filePath = openFileDialog.FileName;

// Load the diagram from a file stream
using (Stream myStream = openFileDialog.OpenFile())
{
diagram1.LoadXml(myStream);
}

this.diagram1.Refresh();
}

{% endhighlight %}
{% endtabs %}

### Loading a Diagram from a File with Exception Handling

{% tabs %}
{% highlight c# %}

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
openFileDialog.DefaultExt = "*.xml";
openFileDialog.Title = "Open Diagram";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string filePath = openFileDialog.FileName;

// Load the diagram from the file path with exception handling
diagram1.LoadXml(filePath, out Exception exception);

this.diagram1.Refresh();
}

{% endhighlight %}
{% endtabs %}

### Loading a Diagram from a Stream with Exception Handling

{% tabs %}
{% highlight c# %}

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
openFileDialog.DefaultExt = "*.xml";
openFileDialog.Title = "Open Diagram";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string filePath = openFileDialog.FileName;

// Load the diagram from the stream with exception handling
using (Stream myStream = openFileDialog.OpenFile())
{
diagram1.LoadXml(myStream, out Exception ex);
}

this.diagram1.Refresh();
}

{% endhighlight %}
{% endtabs %}

## Checking if the Diagram's Model is Modified

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.

{% tabs %}
{% highlight c# %}
if (diagram1 != null && diagram1.Model.Modified)
{
MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show(
"Do you want to save the Diagram?",
"SfDiagram",
MessageBoxButton.YesNo);
if (messageBoxResult == MessageBoxResult.Yes)
{
SaveDiagram();
}
}
{% endhighlight %}
{% endtabs %}
53 changes: 53 additions & 0 deletions WindowsForms/Diagram/Serialization/Palette-Serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
layout: post
title: Palette Serialization in Windows Forms Diagram control | Syncfusion
description: Learn about Palette Serialization support in Syncfusion Windows Forms Diagram control and more details.
platform: windowsforms
control: Diagram
documentation: ug
---

# Palette Serialization in Windows Forms Diagram

Palette serialization allows users to save and load the state of a palette, including all its symbols and their properties. This makes it easier to persist the palette across sessions or share it between applications.

### Saving a Palette

In a palette, the [DataContractSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-8.0) is used for serialization. The functionalities provided by `DataContractSerializer` apply to palette serialization, supporting saving the palette to an XML file while preserving all symbols. The palette's state is fully maintained during serialization.

{% tabs %}
{% highlight c# %}

SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
{
SymbolPalette currentSymbolPalette = paletteGroupBar1.CurrentSymbolPalette;
string strSavePath = saveFileDialog.FileName;
if (currentSymbolPalette != null)
{
currentSymbolPalette.SavePalette(strSavePath);
}
}

{% endhighlight %}
{% endtabs %}


### Loading Palettes Using DataContractSerializer

Loading a palette from an XML file involves deserializing the XML data back into a palette object, ensuring that all symbols and their properties are accurately restored.

{% tabs %}
{% highlight c# %}

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XML Diagram (*.xml)|*.xml|All files (*.*)|*.*";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
string strFileName = openFileDialog.FileName;
paletteGroupBar1.LoadPalette(strFileName);
}

{% endhighlight %}
{% endtabs %}