自定义TXmlIniFile方法会创建错误

时间:2015-02-24 14:58:51

标签: delphi delphi-xe7

我创建了这个包含从TXmlIniFile派生的类的单元:

unit PAXMLIniFile;

interface

uses
  System.SysUtils,
  System.IniFiles,
  Xml.XmlDoc,
  Xml.XMLIntf,
  Xml.XMLIniFile;

type
  TMyStandaloneXmlIniFile = class(TXmlIniFile)
  strict private
    FDocument: IXMLDocument;
    FFileName: string;
  public
    constructor Create(const AFileName: string);
    procedure UpdateFile; override;
  end;

implementation

constructor TMyStandaloneXmlIniFile.Create(const AFileName: string);
begin
  if FileExists(AFileName) then
    FDocument := LoadXMLDocument(AFileName)
  else
  begin
    FDocument := NewXMLDocument;
    FDocument.Options := FDocument.Options + [doNodeAutoIndent];
  end;
  if FDocument.DocumentElement = nil then
    FDocument.DocumentElement := FDocument.CreateNode('Sections');
  TCustomIniFile(Self).Create(AFileName); 
  FFileName := AFileName;
  inherited Create(FDocument.DocumentElement);
end;

procedure TMyStandaloneXmlIniFile.UpdateFile;
begin
  FDocument.SaveToFile(FFileName);
end;

end.

现在每当我调用 UpdateFile 方法时,我都会收到一条错误消息"错误的参数"。这有什么不对?

以下是测试程序:

program BonkersTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, PAXMLIniFile, Winapi.ActiveX;

var
  MyStandaloneXmlIniFile: TMyStandaloneXmlIniFile;
  MyXmlFile: string;

begin
  try
    CoInitialize(nil);
    MyXmlFile := ChangeFileExt(ParamStr(0), '.xml');
    MyStandaloneXmlIniFile := TMyStandaloneXmlIniFile.Create(MyXmlFile);
    try
      MyStandaloneXmlIniFile.WriteString('Section1', 'Ident1', 'Value1');
      MyStandaloneXmlIniFile.UpdateFile;
    finally
      MyStandaloneXmlIniFile.Free;
    end;
    CoUninitialize;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

0 个答案:

没有答案