08-04-2015 05:41 AM
Hello,
It try to serialize a scattergraph, to save all the properties modified by the user.
I have some returned errors that say a lot of parts of properties are not serializable. I try with a visual studio textbox and it works fine.
Is there a solution to serialize a scattergraph ?
Regards
08-05-2015 06:33 PM - edited 08-05-2015 06:34 PM
Hi, virgule
In the future, please post exceptions that you are seeing so it is easier for other forum users to confirm whether they are seeing the same error as you. I tried doing the following in the constructor of my main form, and I think I reproduced the error you are reporting:
var stream = File.Open(XmlFilePath, FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite);
var serializer = new XmlSerializer(typeof(ScatterGraph));
serializer.Serialize(stream, this._scatterGraph);
The exception that is thrown is as follows:
System.InvalidOperationException HResult=-2146233079 Message=Cannot serialize member 'System.ComponentModel.Component.Site' of type 'System.ComponentModel.ISite',
see inner exception for more details. Source=System.Xml
The ScatterGraph class derives from System.ComponentModel.Component which contains this ISite member, which is not serializable.
I would recommend first identifying data on the scatter graph that is of interest to you. After you have aggregated the pieces of data you want, create a separate class which holds the data. When you want to serialize the scatter graph, create an instance of this data class and serialize that instead.
08-06-2015 01:30 AM
Hi, D_Cubed
Sorry for the post exceptions and thank for your reply.
Can you give an example of aggregated pieces of data and how to create a separate class which holds the data ?
(An example of the scatterplot or axes class wich are implemented in the primary class scattergraph)
Regards
08-06-2015 08:44 AM - edited 08-06-2015 08:45 AM
Hi, virgule
You will need to choose what parts of the graph you are interested in serializing. I have attached a simple example that shows a serialization model class I created for use in serialization.
The serialization model looks like the following:
public class ScatterGraphSerializationModel
{
public ScatterGraphSerializationModel()
: this(new ScatterGraph())
{
}
public ScatterGraphSerializationModel(ScatterGraph scatterGraph)
{
// Store the scatter graph's identifier.
Name = scatterGraph.Name;
// Store the first scatter graph x-axis mode.
XAxisMode = scatterGraph.XAxes[0].Mode;
// ...
// Store more properties.
// ...
// Store every plot's x- and y-data.
foreach (var plot in scatterGraph.Plots)
{
Plots.Add(new XYData { XValues = ((ScatterPlot)plot).GetXData(), YValues = ((ScatterPlot)plot).GetYData() });
}
}
public string Name { get; set; }
public AxisMode XAxisMode { get; set; }
// ...
// Add more public properties to the serializable model.
// ...
public List<XYData> Plots
{
get
{
if (_plots == null)
_plots = new List<XYData>();
return _plots;
}
set { _plots = value; }
}
public class XYData
{
public double[] XValues { get; set; }
public double[] YValues { get; set; }
}
private List<XYData> _plots;
}
The example model stores the name of the graph, the mode for the x-axis, and the plot data on the graph. You will need to create your own model of data you wish to serialize.
Thanks,