NMath User's Guide

TOC | Previous | Next | Index

51.3 XML Serialization (.NET, C#, CSharp, VB, Visual Basic, F#)

XML serialization in .NET does not make use of CLR Formatter classes, as do binary serialization (Section 51.1) and SOAP serialization (Section 51.2). Instead, the framework provides the System.Xml.Serialization.XmlSerializer class for persisting to XML documents.

However, because NMath data classes implement the IEnumerable interface, XmlSerializer persists only the enumerated data. Thus, though a matrix or vector object can be serialized in XML, it cannot be restored.

If you want to serialize and deserialize NMath objects in XML format, you can easily overcome this limitation by writing a simple wrapper class that contains all the information necessary to restore the object, without implementing IEnumerable. For example, this code defines an MyNameSpace.MyWrapper class that wraps a DoubleMatrix:

Code Example – C# XML serialization

using CenterSpace.NMath.Core;

namespace MyNamespace
  public class MyWrapper
    public int Rows;
    public int Columns;
    public StorageType Storage = StorageType.ColumnMajor;
    public double[] Data;

    public MyWrapper() {}

    public MyWrapper( DoubleMatrix A )
      Rows = A.Rows;
      Columns = A.Cols;
      DoubleMatrix B = (DoubleMatrix)A.Clone();
      Data = B.DataBlock.Data;

  }  // class

} // namespace

Code Example – XML binary serialization

Imports CenterSpace.NMath.Core

Namespace MyNamespace

Public Class MyWrapper

  Public Rows As Integer
  Public Columns As Integer
  Public Storage As StorageType = StorageType.ColumnMajor
  Public Data() As Double

  Public Sub New()
  End Sub

  Public Sub New(A As DoubleMatrix)
    Rows = A.Rows
    Columns = A.Cols
    Dim B As DoubleMatrix = CType(A.Clone(), DoubleMatrix)
    Data = B.DataBlock.Data
  End Sub

End Class

End Namespace

Note that the constructor uses the Clone() method to ensure that the data is not referenced by any other objects, and that it is in contiguous storage.

You could then use the wrapper class to serialize a matrix object, as shown below:

Code Example – C# XML serialization

using System.IO;
using System.Xml.Serialization;
using MyNamespace;

var A = new DoubleMatrix( "3x3[1 2 3 4 5 6 7 8 9]" );
var AWrap = new MyWrapper( A );

var x = new XmlSerializer( typeof( MyWrapper ) );
FileStream s = File.Create( "myData.xml" );
x.Serialize( s, A );

Code Example – VB XML serialization

Imports System.IO
Imports System.Xml.Serialization
Imports MyNamespace

Dim A As New DoubleMatrix("3x3[1 2 3 4 5 6 7 8 9]")
Dim AWrap As New MyWrapper(A)

Dim X As New XmlSerializer(GetType(MyWrapper))
Dim S As FileStream = File.Create("myData.xml")
X.Serialize(S, W)

To restore the object:

Code Example – C# XML serialization

s = File.OpenRead( "myData.xml" );
MyWrapper AWrap = (MyWrapper)x.Deserialize( s );
var A = new DoubleMatrix( AWrap.Rows, AWrap.Columns, 
  AWrap.Data, AWrap.Storage );

Code Example – VB XML serialization

S = File.OpenRead("myData.xml")
Dim AWrap As MyWrapper = CType(X.Deserialize(S), MyWrapper)
Dim A As New DoubleMatrix(AWrap.Rows, AWrap.Columns,
  AWrap.Data, AWrap.Storage)