Posted Friday, June 17, 2005 12:21 AM
|
|
|
|
Hello,
I’m using the .NET Matrix Library. I have one question, and I can’t find the answer.
Is there a way to store a matrix on disk, and read it again from the disc back to the application? I know that I can use the .ToString method to save the matrix in a text file, but how can I then read the matrix back to my application?
I want to save the matrix on disc because I don’t want to create the matrix always from scratch.
Thank you for your answer.
|
|
Posted Friday, June 17, 2005 12:29 AM
|
|
|
|
Hi
It can be done very easily using either XML or Binary Serialization. Please review the following code example:
private void SaveAndLoadDemo()
{
// Create a matrix object
Matrix myMatrix = new Matrix(4, 4);
myMatrix.FillRandom();
// Print the matrix
Console.WriteLine(myMatrix);
// Save the matrix to an xml file
SaveXML(myMatrix, "C:\\MyMatrix.xml");
// Open the saved matrix into another variable
Matrix mySavedMatrix = LoadXML("C:\\MyMatrix.xml");
Console.WriteLine(mySavedMatrix);
}
// Using XML Serialization to save a matrix
private void SaveXML(Matrix matrix, string filePath)
{
//Open a file stream
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create) ;
// Create a xml Serializer object
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Bluebit.MatrixLibrary.Matrix));
//Save the matrix
xmlSer.Serialize(fs, matrix);
// Close the file stream
fs.Close();
}
// Load a matrix from an XML file
private Matrix LoadXML(string filePath)
{
//Open the XML file
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open) ;
// First create a xml Serializer object
System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Matrix));
// Deserialize the Matrix object
Matrix matrix = (Matrix) xmlSer.Deserialize(fs);
// Close the file stream
fs.Close();
// Return the matrix object
return matrix;
}
// Using Binary Serialization to save a matrix
private void SaveBinary(Matrix matrix, string filePath)
{
//Open a file stream
System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create) ;
// Initialize a BinnaryFormatter object
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFor = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
//Save the matrix
binFor.Serialize(fs, matrix);
// Close the file stream
fs.Close();
}
// Load a matrix from a binary file
private Matrix LoadBinary(string filePath)
{
//Open a file stream
System.IO.FileStream fs = new System.IO.FileStream(filePath,System.IO.FileMode.Open) ;
// Initialize a BinnaryFormatter object
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFor = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
// Deserialize the Matrix object
Matrix matrix = (Matrix) binFor.Deserialize(fs);
// Close the file stream
fs.Close();
// Return the matrix object
return matrix;
}
Trifon Triantafillidis | Lead Developer |
|
|
|
Posted Thursday, July 21, 2005 12:03 PM
|
|
|
|
SaveXML(myMatrix, "C:\\MyMatrix.xml");
What is the same code to save matrix but in vb.net?
|
|
Posted Monday, July 25, 2005 1:26 AM
|
|
|
|
guille,
here is the same code in vb.net
Private Sub SaveAndLoadDemo()
'Create a matrix object
Dim myMatrix = New Matrix(4, 4)
myMatrix.FillRandom()
'Print the matrix
Console.WriteLine(myMatrix)
'Save the matrix to an xml file
SaveXML(myMatrix, "C:\\MyMatrix.xml")
'Open the saved matrix into another variable
Dim mySavedMatrix As Matrix = LoadXML("C:\\MyMatrix.xml")
Console.WriteLine(mySavedMatrix)
End Sub
'Using XML Serialization to save a matrix
Private Sub SaveXML(ByVal matrix As Matrix, ByVal filePath As String)
'Open a file stream
Dim fs As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Create)
'Create a xml Serializer object
Dim xmlSer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(GetType(Bluebit.MatrixLibrary.Matrix))
'Save the matrix
xmlSer.Serialize(fs, matrix)
'Close the file stream
fs.Close()
End Sub
'Load a matrix from an XML file
Private Function LoadXML(ByVal filePath As String) As Matrix
'Open the XML file
Dim FS As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Open)
'First create a xml Serializer objecT
Dim xmlSer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(GetType(Bluebit.MatrixLibrary.Matrix))
'Deserialize the Matrix object
Dim matrix As Matrix = CType(xmlSer.Deserialize(FS), Bluebit.MatrixLibrary.Matrix)
'Close the file stream
FS.Close()
'Return the matrix object
Return matrix
End Function
'Using Binary Serialization to save a matrix
Private Sub SaveBinary(ByVal matrix As Matrix, ByVal filePath As String)
'Open a file stream
Dim fs As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Create)
'Initialize a BinnaryFormatter object
Dim binFor As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
'Save the matrix
binFor.Serialize(fs, matrix)
'Close the file stream
fs.Close()
End Sub
'Load a matrix from a binary file
Private Function LoadBinary(ByVal filePath As String) As Matrix
'Open a file stream
Dim fs As System.IO.FileStream = New System.IO.FileStream(filePath, System.IO.FileMode.Open)
'Initialize a BinnaryFormatter object
Dim binFor As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
'Deserialize the Matrix object
Dim matrix As Matrix = CType(binFor.Deserialize(fs), Bluebit.MatrixLibrary.Matrix)
'Close the file stream
fs.Close()
'Return the matrix object
Return matrix
End Function
Trifon Triantafillidis | Lead Developer |
|
|
|
|
|