Xceed Chart for WinForms v4.4 Documentation
Serialization

Welcome to Xceed Chart for WinForms v4.4 > User Guide > Serialization

It's often very useful to be able to save the state of the component to a file or memory stream. For example, you may want to implement a custom Undo feature to allow your user to revert a chart to some previous state, or you may wish to create template galleries with frequently used charts so that you do not have to write the code to configure them again. 

Xceed Chart for WinForms supports binary serialization of the chart state. Furthermore, the state files generated by Windows Forms and ASP.NET (WebForms) are compatible, meaning that you can use state files generated by the Windows Forms control in the ASP.NET control and vice versa. In order to import or export the chart state in binary format, you must gain access to a ChartControlState object for Windows Forms or an ChartServerControlState object for ASP.NET (WebForms). This is achieved with the ChartControl.State and ChartServerControl. State properties exposed by the Windows Forms and WebForms control, respectively. Both objects inherit from ChartControlStateBase and therefore share common functionality. However, neither object adds public members, so you'll always work with the methods exposed by the ChartControlStateBase object.

Serialization with Files 

The ChartControlStateBase object exposes two methods called SaveToFile and LoadFromFile, which allow you to write and read binary state data to and from a file. 

To serialize the component state to a file, you must use the following code:

VB.NET  
chartControl1.State.SaveToFile("c:\temp\chartstate.xsv")

C#  
chartControl1.State.SaveToFile("c:\\temp\\chartstate.xsv);

This will create a file called chartstate.xsv containing the binary state of the component. If you want to revert to this state at some point in your program, you must use the LoadFromFile function:

VB.NET  
chartControl.State.LoadFromFile("c:\temp\chartstate.xsv")

C#  
chartControl.State.LoadFromFile("c:\\temp\\chartstate.xsv");

Note that the extension of the file is not important in this process. If you pass a file that does not contain a valid state, the LoadFromFile function will throw an exception. If you use the component in ASP.NET applications, you must make sure that the ASP.NET account on the machine has sufficient rights to write to the directory containing the state file.

Serialization with Streams 

Alternatively, you can load and save the component state in any type of stream. This allows you to build more complex applications. For example, consider that you're developing an Excel-like application, which uses Xceed Chart for WinForms to present data. In this case, you'll probably have a file format where you store the different objects in the spreadsheet: cell values, chart, drawing, etc. In such cases, the LoadFromStream and SaveToStream functions of the ChartControlStateBase can come in handy. The following code snippets illustrate how to save and load the component state from a stream: 

VB.NET  

Dim memoryStream As MemoryStream = New MemoryStream()
chartControl1.State.SaveToStream(memoryStream)
C#  
MemoryStream memoryStream = new MemoryStream();
chartControl1.State.SaveToStream(memoryStream);

To load the state from the same stream you must first reset the stream:

VB.NET  

memoryStream.Seek(0, SeekOrigin.Begin)
chartControl1.State.LoadFromStream(memoryStream)
C#  
memoryStream.Seek(0, SeekOrigin.Begin);
chartControl1.State.LoadFromStream(memoryStream);

Serialization with XML 

In recent years, XML has become the primary language for interchanging human readable data and for building distributed applications. Xceed Chart for WinForms can export its state as a xml element containing binary data, which allows you to embed chart state information in the XML files generated by your application. The functions of the ChartControlStateBase object involved in this process are SaveStateToXml and LoadStateFromXml. The first one returns an xml element with the following format:

<state>binary data</state>

The LoadStateFromXml accepts a string with the same formatting.

The following code saves and then loads the state of the component in an XML element:

VB.NET  

Dim sXmlState As String = chartControl.State.SaveStateToXml()
chartControl1.State.LoadStateFromXml(sXmlState)
C#  
String sXmlState = chartControl1.State.SaveStateToXml();
chartControl1.State.LoadStateFromXml(sXmlState);

See also 

ChartControlStateBase | ChartServerControl | ChartControl