NMath User's Guide

TOC | Previous | Next | Index

51.2 SOAP Serialization (.NET, C#, CSharp, VB, Visual Basic, F#)

The System.Runtime.Serialization.Formatters.Soap.SoapFormatter class provides Serialize() and Deserialize() methods for persisting an object in SOAP format to a given stream. For example, this code serializes a FloatComplexTriDiagFact object to a file:

Code Example – C# SOAP serialization

using System.IO;
using System.Runtime.Serialization.Formatters.Soap;



int rows = 8, cols = 8;
FloatComplexVector data =
  new FloatComplexVector( cols*3, new RandGenUniform(-1, 1) );
var A =
  new FloatComplexTriDiagMatrix( data, rows, cols );
var F = new FloatComplexTriDiagFact( A );



FileStream xmlStream = File.Create( "myData.xml" );
var xmlFormatter = new SoapFormatter();
xmlFormatter.Serialize( xmlStream, F );
xmlStream.Close();

Code Example – VB SOAP serialization

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Soap



Dim Rows As Integer = 8
Dim Cols As Integer = 8



Dim Data As New FloatComplexVector(Cols * 3,
  New RandGenUniform(-1.0, 1.0))
Dim A As New FloatComplexTriDiagMatrix(Data, Rows, Cols)
Dim F As New FloatComplexTriDiagFact(A)



Dim XMLStream As FileStream = File.Create("myData.xml")
Dim XMLFormatter As New SoapFormatter()
XMLFormatter.Serialize(XMLStream, F)
XMLStream.Close()

This code restores the objects from the file:

Code Example – C# SOAP serialization

xmlStream = File.OpenRead( "myData.xml");
var F = 
  (FloatComplexTriDiagFact)xmlFormatter.Deserialize( xmlStream );



xmlStream.Close();
File.Delete( "myData.xml" );

Code Example – VB SOAP serialization

XMLStream = File.OpenRead("myData.xml")
Dim F = CType(XMLFormatter.Deserialize(XMLStream), 
  FloatComplexTriDiagMatrix)



XMLStream.Close()
File.Delete("myData.xml")

Top

Top