Pergunta

I've just installed Delphi 7 for Personal Use and I'm trying to convert a delphi ClientDataSet file to dfXMLUTF8. All I really need is the xml structure. This site suggests that running 4 lines of code will generate the output I desire. Note however that My file is .cds, not .dat as in the example. I don't know if this makes a difference.

ClientDataSet1.Active := false;
ClientDataSet1.CreateDataSet;
ClientDataSet1.LoadFromFile('MyBinaryFile.dat');
ClientDataSet1.SaveToFile('MyXMLFile.XML', dfXMLUTF8);

I'm receiving errors and since I've never really used Delphi before and I'm hoping somebody who knows what they are doing could just post the very short xml structure for me. Here's the file(486 bytes) I'm working with. I will award correct answer to the first poster with full dfXMLUTF8 output. Thanks!

Update:

Okay I just started the project from scratch and I think I have it somewhat setup. I have added uses DBClient;, var ClientDataSet1: TClientDataSet, and lastly the procedure:

ClientDataSet1.LoadFromFile('C:\Documents and Settings\XPMUser\Desktop\DelphiCDS\Master.cds');
  ClientDataSet1.SaveToFile('C:\Documents and Settings\XPMUser\Desktop\DelphiCDS\output.XML', dfXMLUTF8);

It throws the following error:

Access violation at address 004588B6 in module 'Project.exe'. Read of address 00000000.

Update2:

Here's what I ended up with:

unit Script3;

interface

uses
  Forms, DBClient;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;
  CDS: TClientDataSet;

implementation
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  CDS := TClientDataSet.Create(nil);
  try
    CDS.LoadFromFile('.\input.cds');
    CDS.SaveToFile('.\output.xml', dfXMLUTF8);
  finally
    CDS.Free;
  end;
end;

end.
Foi útil?

Solução

You're doing much more than you have to in order to accomplish the conversion. With the presumption that MyBinaryFile.dat is actually a binary format Delphi TClientDataSet file, these two lines of code (with no additional setup) will work:

ClientDataSet1.LoadFromFile('D:\Temp\MyBinaryFile.dat');
ClientDataSet1.SaveToFile('D:\Temp\MyXMLFile.xml', dfXMLUTF8);

Without knowing more about what your MyBinaryFile.dat is, and no information about the errors you're getting, it's pretty difficult to provide more information. Tested and working with the standard animals.cds in Delphi 7 (from C:\Program Files\Common Files\Borland Shared\Data in a standard D7 install on a Windowx XP virtual machine).

It seems, from your comment below, that the actual problem you're having is a compiler error about an unidentified identifier TClientDataSet when you try and create it in code. If you have a high enough SKU for Delphi 7 that includes TClientDataSet (IIRC, Professional and higher), you can just add DBClient to your uses clause:

implementation

uses 
  DBClient;

procedure TForm1.FormShow(Sender: TObject);
var
  CDS: TClientDataSet;
begin
  CDS := TClientDataSet.Create(nil);
  try
    CDS.LoadFromFile('MyBinaryFile.dat');
    CDS.SaveToFile('MyXMLFile.xml', dfXMLUTF8);
  finally
    CDS.Free;
  end;
end;

Or, better yet, if you're creating a VCL Forms application, just drop a TClientDataSet on your form; you'll find it on the DataAccess tab in the component palette.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top