SciChart WPF 2D Charts > Advanced 2D Charting Topics > Serialization
Serialization

SciChart supports serialization of chart settings to XML, which allows you to inspect the chart’s structure, debug issues, or store configurations for later reconstruction.

This mechanism is also used internally during export with custom parameters, making it a valuable tool for export debugging. You can use it to verify whether all expected properties and chart elements are correctly serialized before export.

Serialization of Chart Settings

To serialize a chart, use the following code. This code will serialize a SciChartSurface and its settings to MemoryStream but you can use any other stream e.g. FileStream to save XML to a file.

Serialization of Chart Settings
Copy Code
var stream = new MemoryStream();
var serializer = new XmlSerializer(typeof(SciChartSurface));
serializer.Serialize(stream, SciChartSurface);

Deserialization of Chart Settings

To restore a chart from XML use the following code:

Deserialization of Chart Settings
Copy Code
String xmlString = … // assuming you have the XML string in memory
var stream = new MemoryStream(Encoding.ASCII.GetBytes(xmlString));
var serializer = new XmlSerializer(typeof(SciChartSurface));
stream.Position = 0;
var chart = (SciChartSurface)serializer.Deserialize(stream);
chart.RenderableSeries.First().DataSeries = DataSeries;

Reading a Serialized Chart into an XML String

The following example demonstrates how to convert a chart into an XML string using SciChart’s internal serialization mechanism:

Converting a chart into an XML string
Copy Code
var stream = new MemoryStream();
var serializer = new XmlSerializer(typeof(SciChartSurface));
serializer.Serialize(stream, SciChartSurface);
stream.Position = 0;
var serializedString = new StreamReader(stream).ReadToEnd();

Serialization of Chart Data

Currently SciChart does not support serialization of chart data out of the box, however, if you see Accessing X,Y Values (read-only)  , you can access the DataSeries.XValues and DataSeries.YValues as a list and persist these as you wish.

Example of Serialization Output

An example of serialization output is included below.

Example of Serialization Output
Copy Code
<SciChartSurface ClipOverlayAnnotations="True" ClipUnderlayAnnotations="True" ClipModifierSurface="True" Theme="">
  <RenderableSeries>
    <FastLineRenderableSeries
             Type="SciChart.Charting.Visuals.RenderableSeries.FastLineRenderableSeries, 
                   SciChart.Charting, Version=4.0.0.5900, Culture=neutral,
                    PublicKeyToken=b55dd9efe817e823"
             XAxisId="DefaultAxisId"
             YAxisId="DefaultAxisId"
              IsVisible="True"
             StrokeThickness="1" AntiAliasing="True" ResamplingMode="Auto"
             Stroke="FF,C6,E6,FF" DrawNaNAs="Gaps" IsDigitalLine="False" />
  </RenderableSeries>
  <XAxes Type="SciChart.Charting.Model.AxisCollection, SciChart.Charting,
               Version=4.0.0.5900, Culture=neutral, PublicKeyToken=b55dd9efe817e823">
    <DateTimeAxis Type="SciChart.Charting.Visuals.Axes.DateTimeAxis, SciChart.Charting,
                   Version=4.0.0.5900, Culture=neutral, PublicKeyToken=b55dd9efe817e823"
                  AutoRange="Once" AutoTicks="True" AxisAlignment="Bottom"
                  DrawMajorBands="True" DrawMajorTicks="True" DrawLabels="True"
                  DrawMinorTicks="True" DrawMajorGridLines="True" DrawMinorGridLines="True"
                  Id="xAxis1"
                  GrowBy="SciChart.Data.Model.DoubleRange,0,0" MinorsPerMajor="5"
                   MaxAutoTicks="10" FlipCoordinates="False" />
    <DateTimeAxis Type="SciChart.Charting.Visuals.Axes.DateTimeAxis, SciChart.Charting,
                  Version=4.0.0.5900, Culture=neutral, PublicKeyToken=b55dd9efe817e823"
                  AutoRange="Once" AutoTicks="True" AxisAlignment="Bottom"
                  DrawMajorBands="False" DrawMajorTicks="True" DrawLabels="True"
                 DrawMinorTicks="True" DrawMajorGridLines="False"           
                 DrawMinorGridLines="False" Id="xAxis2"
                 GrowBy="SciChart.Data.Model.DoubleRange,0,0" MinorsPerMajor="5"
                 MaxAutoTicks="10" FlipCoordinates="False" />
  </XAxes>
  <YAxes Type="SciChart.Charting.Model.AxisCollection, SciChart.Charting,
                 Version=4.0.0.5900, Culture=neutral, PublicKeyToken=b55dd9efe817e823">
    <NumericAxis Type="SciChart.Charting.Visuals.Axes.NumericAxis, SciChart.Charting,
                 Version=4.0.0.5900, Culture=neutral, PublicKeyToken=b55dd9efe817e823"
                  AutoRange="Once" AutoTicks="True" AxisAlignment="Right"
                  DrawMajorBands="True" DrawMajorTicks="True" DrawLabels="True"
                 DrawMinorTicks="True" DrawMajorGridLines="True" DrawMinorGridLines="True"
                 Id="yAxis1" GrowBy="SciChart.Data.Model.DoubleRange,0,0"
                 MinorsPerMajor="5" MaxAutoTicks="10" FlipCoordinates="False" />
    <NumericAxis Type="SciChart.Charting.Visuals.Axes.NumericAxis, SciChart.Charting,
                 Version=4.0.0.5900, Culture=neutral, PublicKeyToken=b55dd9efe817e823"
                 AutoRange="Once" AutoTicks="True" AxisAlignment="Right"
                  DrawMajorBands="False" DrawMajorTicks="True" DrawLabels="True"
                 DrawMinorTicks="True" DrawMajorGridLines="False"
                 DrawMinorGridLines="False" Id="yAxis2"
                  GrowBy="SciChart.Data.Model.DoubleRange,0,0" MinorsPerMajor="5"
                 MaxAutoTicks="10" FlipCoordinates="False" />
  </YAxes>
  <Annotations Type="SciChart.Charting.Visuals.Annotations.AnnotationCollection,
               SciChart.Charting, Version=4.0.0.5900, Culture=neutral,
               PublicKeyToken=b55dd9efe817e823">
    <TextAnnotation Type="SciChart.Charting.Visuals.Annotations.TextAnnotation,
               SciChart.Charting, Version=4.0.0.5900, Culture=neutral,
                PublicKeyToken=b55dd9efe817e823" IsEditable="False" IsHidden="False"
               IsSelected="False" XAxisId="DefaultAxisId" YAxisId="DefaultAxisId"
               CoordinateMode="Absolute" Background="0,FF,FF,FF" BorderThickness="0,0,0,0"
                Foreground="FF,A6,A7,AC" FontSize="12" FontWeight="Normal"
               FontFamily="Segoe UI"
               FontStyle="Normal" Text="" TextAlignment="Left" />
  </Annotations>
  <ChartModifier Type="SciChart.Charting.ChartModifiers.ZoomExtentsModifier,
                 SciChart.Charting, Version=4.0.0.5900, Culture=neutral,
                 PublicKeyToken=b55dd9efe817e823"
                 ExecuteOn="MouseDoubleClick" ReceiveHandledEvents="True" IsEnabled="True"
                 XyDirection="XYDirection" IsAnimated="True" />
</SciChartSurface>

Serializing and Deserializing Across SciChart Versions

You will notice many types are qualified by their fully-qualified assembly name. This is unfortunately unavoidable as serialization across assemblies requires that the fully-qualified name is included in the serialized output.

What we suggest if you need to serialize and deserialize across version(s) is to pre-process the fully XML string and replace the fully qualified assembly names.

For instance:

Find: SciChart.Charting.Model.AxisCollection, SciChart.Charting,
                 Version=4.0.0.5900, Culture=neutral, PublicKeyToken=b55dd9efe817e823

Replace: SciChart.Charting.Model.AxisCollection, SciChart.Charting,
                 Version=4.0.1.6200, Culture=neutral, PublicKeyToken=b55dd9efe817e823

 

This will upgrade the XML to be compatible to a newer version of SciChart.

 

See Also