a question about binary serialization


T

Tony Johansson

I want to Binary serialize these data that exist in class DiagramData that
is listed below.
1. private List<PointF> points = new List<PointF>();
2. private int xIntervals = 10;
3. private int yIntervals = 10;
4. private double xIntvValue = 10.0;
5. private double yIntvValue = 10.0;´


In the code below named SaveToFile and SaveToolStripMenuItem_Click I can
serialize this data
List<PointF> points = new List<PointF>();
without any problem.
I can also load it back without any problem by using method
openToolStripMenuItem_Click listed below.
I have also listed the methods that take care of the actual binary
serialization and binary desrialization.


So as a summary how do I add binary serialization for these data
private int xIntervals = 10;
private int yIntervals = 10;
private double xIntvValue = 10.0;
private double yIntvValue = 10.0;
In addition to the already working binary serialization of List<PointF>
points = new List<PointF>();


class DiagramData
{
private List<PointF> points = new List<PointF>();
private int xIntervals = 10;
private int yIntervals = 10;
private double xIntvValue = 10.0;
private double yIntvValue = 10.0;
private bool dataSaved = true;

public bool DataSaved
{
get { return dataSaved; }
set { dataSaved = value; }
}

public List<PointF> Points
{
get { return points; }
set { points = value; }
}

public int YIntervals
{
get { return yIntervals; }
set { yIntervals = value; }
}

public int XIntervals
{
get { return xIntervals; }
set { xIntervals = value; }
}

public double YIntvValue
{
get { return yIntvValue; }
set { yIntvValue = value; }
}

public double XIntvValue
{
get { return xIntvValue; }
set { xIntvValue = value; }
}

public void Add(PointF pointf)
{
points.Add(pointf);
}

public int Count
{
get { return points.Count; }
}
}

public partial class MainForm : Form
{
private void SaveToFile()
{
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = "Data";
saveFileDialog.Title = "Save File";
saveFileDialog.ValidateNames = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.InitialDirectory =
System.IO.Directory.GetCurrentDirectory();
saveFileDialog.Filter = "Data files (*.data)|*.data|All Files|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
chosenFileName = saveFileDialog.FileName;
BinarySerialization.BinaryFileSerialize(data.Points.ToArray(),
chosenFileName);

//Indikera att data är sparad
data.DataSaved = true;
}
}

private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (data.Count == 0)
{
return;
}
if (chosenFileName == string.Empty)
{
SaveToFile();
}
else
{
BinarySerialization.BinaryFileSerialize(data.Points.ToArray(),
chosenFileName);
}
}
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog.AddExtension = true;
openFileDialog.DefaultExt = "Data";
openFileDialog.Title = "Open File";
openFileDialog.ValidateNames = true;
openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
openFileDialog.Filter = "Data files (*.data)|*.data|All Files|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
data.Points =
BinarySerialization.BinaryFileDeserialize<PointF[]>(openFileDialog.FileName).ToList();
pnlDiagram.Invalidate();
}
}


// Here is the method that handle the binary serialization and binary
deserialization
public static void BinaryFileSerialize(Object obj, string filePath)
{
try
{
using (FileStream fileStream = new FileStream(filePath,
FileMode.Create))
{
new BinaryFormatter().Serialize(fileStream, obj);
}
}
catch(Exception)
{
throw;
}
}


public static T BinaryFileDeserialize<T>(string filePath)
{
Object obj = null;
try
{
if (File.Exists(filePath) == false)
throw new FileNotFoundException("The file" + " was not
found.", filePath);

using (FileStream fileStream = new FileStream(filePath,
FileMode.Open))
{
obj = new BinaryFormatter().Deserialize(fileStream);
}
}
catch
{
throw;
}

return (T)obj;
}

//Tony
 
Ad

Advertisements

T

Tony Johansson

I solved it myself. I just binary serialized the class DiagramData
I also had to add the serializable attribut to the class DiagramData.

//Tony

Tony Johansson said:
I want to Binary serialize these data that exist in class DiagramData that
is listed below.
1. private List<PointF> points = new List<PointF>();
2. private int xIntervals = 10;
3. private int yIntervals = 10;
4. private double xIntvValue = 10.0;
5. private double yIntvValue = 10.0;´


In the code below named SaveToFile and SaveToolStripMenuItem_Click I can
serialize this data
List<PointF> points = new List<PointF>();
without any problem.
I can also load it back without any problem by using method
openToolStripMenuItem_Click listed below.
I have also listed the methods that take care of the actual binary
serialization and binary desrialization.


So as a summary how do I add binary serialization for these data
private int xIntervals = 10;
private int yIntervals = 10;
private double xIntvValue = 10.0;
private double yIntvValue = 10.0;
In addition to the already working binary serialization of List<PointF>
points = new List<PointF>();


class DiagramData
{
private List<PointF> points = new List<PointF>();
private int xIntervals = 10;
private int yIntervals = 10;
private double xIntvValue = 10.0;
private double yIntvValue = 10.0;
private bool dataSaved = true;

public bool DataSaved
{
get { return dataSaved; }
set { dataSaved = value; }
}

public List<PointF> Points
{
get { return points; }
set { points = value; }
}

public int YIntervals
{
get { return yIntervals; }
set { yIntervals = value; }
}

public int XIntervals
{
get { return xIntervals; }
set { xIntervals = value; }
}

public double YIntvValue
{
get { return yIntvValue; }
set { yIntvValue = value; }
}

public double XIntvValue
{
get { return xIntvValue; }
set { xIntvValue = value; }
}

public void Add(PointF pointf)
{
points.Add(pointf);
}

public int Count
{
get { return points.Count; }
}
}

public partial class MainForm : Form
{
private void SaveToFile()
{
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = "Data";
saveFileDialog.Title = "Save File";
saveFileDialog.ValidateNames = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.InitialDirectory =
System.IO.Directory.GetCurrentDirectory();
saveFileDialog.Filter = "Data files (*.data)|*.data|All
Files|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
chosenFileName = saveFileDialog.FileName;
BinarySerialization.BinaryFileSerialize(data.Points.ToArray(),
chosenFileName);

//Indikera att data är sparad
data.DataSaved = true;
}
}

private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (data.Count == 0)
{
return;
}
if (chosenFileName == string.Empty)
{
SaveToFile();
}
else
{

BinarySerialization.BinaryFileSerialize(data.Points.ToArray(),
chosenFileName);
}
}
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog.AddExtension = true;
openFileDialog.DefaultExt = "Data";
openFileDialog.Title = "Open File";
openFileDialog.ValidateNames = true;
openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
openFileDialog.Filter = "Data files (*.data)|*.data|All Files|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
data.Points =
BinarySerialization.BinaryFileDeserialize<PointF[]>(openFileDialog.FileName).ToList();
pnlDiagram.Invalidate();
}
}


// Here is the method that handle the binary serialization and binary
deserialization
public static void BinaryFileSerialize(Object obj, string filePath)
{
try
{
using (FileStream fileStream = new FileStream(filePath,
FileMode.Create))
{
new BinaryFormatter().Serialize(fileStream, obj);
}
}
catch(Exception)
{
throw;
}
}


public static T BinaryFileDeserialize<T>(string filePath)
{
Object obj = null;
try
{
if (File.Exists(filePath) == false)
throw new FileNotFoundException("The file" + " was not found.",
filePath);

using (FileStream fileStream = new FileStream(filePath,
FileMode.Open))
{
obj = new BinaryFormatter().Deserialize(fileStream);
}
}
catch
{
throw;
}

return (T)obj;
}

//Tony
 
A

Arne Vajhøj

I solved it myself. I just binary serialized the class DiagramData
I also had to add the serializable attribut to the class DiagramData.

Binary serialization is very different from XML serialization.

Binary serialization serializes all fields. You can use
[Serializable] and [NonSerializable] to control what gets
serialized and what not.

XML serialization uses properties.

Arne
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top