Nevron .NET Vision
Chart for .NET / User's Guide / Serialization / Serializing Objects

In This Topic
    Serializing Objects
    In This Topic

    The Serializer has several methods allowing you to serialize object from the chart to a file or stream. The following sections describe this feature. Note that the code is valid for any chart object (label, watermark, legend etc.).

     Serialization to Stream

    You can use the serializer to clone objects or copy state from one chart object to another. The following code creates a chart that is identical to the first chart in the Charts collection:

    C#
    Copy Code
    NControlSerializer serializer = chartControl.Serializer;
    NChart chart = chartControl.Charts[0];
    
    // save the chart to memory stream
    MemoryStream memoryStream = new MemoryStream();
    serializer.SaveToStream(chart, memoryStream, PersistencyFormat.Binary, null);
    
    // go back to the beginning of the stream
    memoryStream.Position = 0;
    
    NChart chart2 = (NChart)serializer.LoadFromStream(typeof(NChart), memoryStream, PersistencyFormat.Binary, null);
    
    Visual Basic
    Copy Code
    Dim serializer As NControlSerializer = chartControl.Serializer
    Dim chart As NChart = chartControl.Charts(0)
    
    ' save the chart to memory stream
    Dim memoryStream As New MemoryStream
    serializer.SaveToStream(chart, memoryStream, PersistencyFormat.Binary, Nothing)
    
    ' go back to the beginning of the stream
    memoryStream.Position = 0
    
    Dim chart2 As NChart = CType(serializer.LoadFromStream(GetType(NChart), memoryStream, PersistencyFormat.Binary, Nothing), NChart)
    

    Notice that you have to reset the stream position after you serialize the object:

    C#
    Copy Code
    // go back to the beginning of the stream
    memoryStream.Position = 0;
    
    Visual Basic
    Copy Code
    ' go back to the beginning of the stream
    memoryStream.Position = 0
    
     Serialization to File

    Alternatively you can serialize the chart to a file:

    C#
    Copy Code
    NControlSerializer serializer = chartControl.Serializer;
    NChart chart = chartControl.Charts[0];
    
    // save the chart to file
    MemoryStream memoryStream = new MemoryStream();
    serializer.SaveToFile(chart, "c:\\temp\\chart.bin", PersistencyFormat.Binary, null);
    
    // load the chart from file
    NChart chart2 = (NChart)serializer.LoadFromFile(typeof(NChart), "c:\\temp\\chart.bin", PersistencyFormat.Binary, null);
    
    Visual Basic
    Copy Code
    Dim serializer As NControlSerializer = chartControl.Serializer
    Dim chart As NChart = chartControl.Charts(0)
    
    ' save the chart to file
    serializer.SaveToFile(chart, "c:\\temp\\chart.bin", PersistencyFormat.Binary, Nothing)
    
    ' load the chart from file
    Dim chart2 As NChart = CType(serializer.LoadFromFile(GetType(NChart), "c:\\temp\\chart.bin", PersistencyFormat.Binary, Nothing), NChart)
    
     Related Examples
    Windows Forms: Serialization\Chart Objects
    See Also