Articles in this section
Category / Section

How to serialize multiple diagram pages in WPF Diagram?

3 mins read

The WPF Diagram supports saving multiple diagram pages as a single package and loading them back as multiple pages. This is achieved by saving each diagram page into an individual file stream using the Save option. These streams are then combined into a single package with a custom “.sfd” extension. When loading, each file is read as a memory stream using the Load method of the diagram.

Code snippet:

//To save multiple diagram pages as single package using stream file.
public void CreateSFDPackage(string packageFilePath, List<string> xamlFiles)
{
   //Saving all diagram pages as individual file as stream using Save method.
   int i = 1;
   foreach (SelectedDiagram diagram in Diagrams)
   {
       using (Stream str = File.Open("Diagram" + i + ".xaml", FileMode.CreateNew))
       {
           xamlFiles.Add("Diagram" + i + ".xaml");
           diagram.Save(str);
       }

       i++;
   }

   //To store all saved diagram files into single package using sfd format.
   using (Package package = Package.Open(packageFilePath, FileMode.Create))
   {
       foreach (var xamlFile in xamlFiles)
       {
           Uri partUri = PackUriHelper.CreatePartUri(new Uri(System.IO.Path.GetFileName(xamlFile), UriKind.Relative));
           PackagePart packagePart = package.CreatePart(partUri, System.Net.Mime.MediaTypeNames.Text.Xml);

           using (FileStream fileStream = new FileStream(xamlFile, FileMode.Open, FileAccess.Read))
           {
               using (Stream partStream = packagePart.GetStream())
               {
                   fileStream.CopyTo(partStream);
               }
           }
       }
   }
}

//To load the saved diagram package into multiple diagram pages using Load() method.
public void OpenSFDPackage(string packageFilePath)
{
   //To clear the diagram items.
   Diagrams.Clear();
   using (Package package = Package.Open(packageFilePath, FileMode.Open, FileAccess.Read))
   {
       foreach (PackagePart part in package.GetParts())
       {
           if (part.ContentType == System.Net.Mime.MediaTypeNames.Text.Xml)
           {
               using (Stream partStream = part.GetStream())
               {
                   Diagrams.Add(new SelectedDiagram() { Name = "DiagramPage" + (Diagrams.Count + 1) });
                   SelectedDiagram.Load(partStream);
               }
           }
       }
   }
} 

View Sample in GitHub

Conclusion

I hope you enjoyed learning about how to serialize multiple diagram pages in WPF Diagram.

You can refer to our WPF Diagram feature tour page to learn about its other groundbreaking feature representations and documentation and how to quickly get started for configuration specifications.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied