SoapFormatter.Deserialize not working "correctly"


2007-02-21 04:10:45 AM
delphi272
I have a class - TTest - with SaveToXML and LoadFromXML routines that call
SoapFormatter.Serialize & SoapFormatter.Deserialize. The SaveToXML works as
expected, however, I am getting strange results with LoadFromXML - within
the routine itself, I can examine the value of Self.FName after the call to
Deserialize and it contains a valid value, after the call FName is blanked!
I'm fairly stumped with this one, so any help would be appreciated. Is it
that Deserialize creates a new object?
Thanks,
Fiachra
program TestSOAP;
{$APPTYPE CONSOLE}
{%DelphiDotNetAssemblyCompiler
'$(SystemRoot)\microsoft.net\framework\v1.1.4322\System.Runtime.Serialization.Formatters.Soap.dll'}
uses
System.IO,
System.Runtime.Serialization.Formatters.Soap;
type
[Serializable]
TTest = class(System.Object)
private
FName: string;
public
procedure LoadFromXML(const Filename: string);
procedure SaveToXML(const Filename: string);
property Name: string read FName write FName;
end;
procedure TTest.LoadFromXML(const Filename: string);
var
fs: FileStream;
Soap: SoapFormatter;
begin
fs := System.IO.File.OpenRead(Filename);
try
Soap := SoapFormatter.Create;
Self := TTest(Soap.Deserialize(fs));
finally
fs.Close;
end;
end;
procedure TTest.SaveToXML(const Filename: string);
var
fs: FileStream;
Soap: SoapFormatter;
begin
fs := FileStream.Create(Filename, FileMode.Create);
try
Soap := SoapFormatter.Create;
Soap.Serialize(fs, Self);
finally
fs.Close;
end;
end;
var
t: TTest;
begin
t := TTest.Create;
t.Name := 'Test1';
t.SaveToXML('c:\abc.xml'); // This works, i.e., creates c:\abc.xml with
"Test1" as the value for Name.
t.Name := '';
t.LoadFromXML('c:\abc.xml');
// Within LoadFromXML I can evalute Self.FName and see "Test1" in it; when
the call ends Self.FName is blanked out!
Console.WriteLine(t.Name);
Console.ReadLine;
end.