troubleshooting Question

Problem in working with XML file

Avatar of coldboy
coldboy asked on
Delphi
5 Comments1 Solution293 ViewsLast Modified:
Hi!
I have some problem with XML file.
I want to built a application that be able to save it's configuration in to a xml file.
So I built a class named TApplicationConfig that can work with XML file.
But When I call GetProperty method of this class, an Error appears and I don't understand why error!
Have a look at my code:
-----------------------------------------------------------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
uses XMLDoc, XmlIntf;
{$R *.dfm}

type
  TApplicationConfig = class(TObject)
  protected
    FXml: TXMLDocument;
    function GetProperty(Name: WideString): WideString;
    procedure SetProperty(Name: WideString; Value: WideString);
  public
    Constructor Create(XmlFileName: String);
    Destructor Destroy; override;
    property _Property[Name: WideString]: WideString read GetProperty write SetProperty;
  end;

constructor TApplicationConfig.Create(XmlFileName: String);
var
  Str: TStrings;
  Msg: String;
begin
  FXml:= TXMLDocument.Create(nil);
  FXml.FileName:= XmlFileName;
  Try
    FXml.Active:= True
  except
    FXml.Active:= False;
    Str:= TStringList.Create;
    Str.Add('<?xml version="1.0" encoding="UTF-8"?>');
    Str.Add('<CONFIG>');
    Str.Add('</CONFIG>');
    try
      Str.SaveToFile(XmlFileName);
      FXml.Active:= True;
    except
      FXml.Active:= False;
      Msg:= Format('Unable to load config from file %s. Access denied ',[XmlFilename]);
      MessageBox(Application.Handle, Pchar(Msg), 'Error', MB_OK OR MB_ICONERROR);
    end;
    Str.Free;
  end;
end;

destructor TApplicationConfig.Destroy;
var Msg: String;
begin
  if FXml.Active then
    Try
      FXml.SaveToFile(FXml.FileName);
    Except
      Msg:= Format('Unable to write config to file %s. Access denied', [FXml.FileName]);
      MessageBox(Application.Handle, PChar(Msg), 'Error', MB_OK OR MB_ICONERROR);
    end;
  FXml.Active:= False;
  FXml.Free;
  inherited;
end;

function TApplicationConfig.GetProperty(Name: WideString): WideString;
var
  c1: integer;
  Root: IXmlNode;
begin
  if FXml.Active then begin
    Root:= FXml.DocumentElement;
    for c1:= 0 to Root.ChildNodes.Count-1 do
      if AnsiCompareStr(Name, Root.ChildNodes[c1].NodeName)=0 then begin
        Result:= Root.ChildNodes[c1].NodeValue;
        Exit;
      end;
  end else Result:= '';
end;

procedure TApplicationConfig.SetProperty(Name: WideString; Value: WideString);
var
  c1: integer;
  Node: IXmlNode;
  Root: IXmlNode;
begin
  if FXml.Active then begin
    Root:= FXml.DocumentElement;
    for c1:= 0 to Root.ChildNodes.Count-1 do
      if AnsiCompareStr(Name, Root.ChildNodes[c1].NodeName)=0 then begin
        Root.ChildNodes[c1].NodeValue:= Value;
        Exit;
      end;
    Node:= FXml.CreateElement(Name, EmptyParam);
    Node.NodeValue:= Value;
    Root.ChildNodes.Add(Node);
  end;
end;

var AppCfg: TApplicationConfig;
procedure TForm1.FormCreate(Sender: TObject);
begin
  AppCfg:= TApplicationConfig.Create('D:\test.xml');
  ShowMessage(AppCfg._Property['LANG']);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  AppCfg.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AppCfg._Property['LANG']:= 'Hehehe';
end;

end.
--------------------------------End------
Thanks for care.
coldboyqn
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros