• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Storing and accessing file paths in xml with Delphi

I have a data file in XML in which I would like to save a file path, retrieve it and replace it programmatically.  I need to see some sample code for doing this using ClientDataSet and/or Data Binding.
0
swiss77
Asked:
swiss77
1 Solution
 
Sinisa VukCommented:
I don't know which version of Delphi you have, but here are some articles which works in older versions:
Database table to XML and back on delphi.about.com
Using_XML_in_Database_Applications (Emb...)
tclientdataset-loading-from-a-xml

... so all thogether:
//save
ClientDataSet1.SaveToFile('C:\XMLDataFile.xml', dfXMLUTF8);

//load 
ClientDataSet1.LoadFromFile('C:\XMLDataFile.xml'); 

Open in new window

0
 
jimyXCommented:
There are two ways that you can work with TClientDataSet and XML. Either to load the XML data and do the updates and then overwrite the whole file with the new changed data, or incorporate the changes in the cds and save when changes done. This later is useful when having huge data to work with.

If you want Load, change, export (same as suggested by Sinisa Vuk above):
1. Drop TClientDataSet as cds, TDataSource, TDBGrid, TEdit as EdFileName and two TButtons (BtnExport and BtnLoad).
2. Link the TDataSource with the cds and the TDBGrid.
3. First Button is to Load xml File, second button is to export. OnClick event write:
procedure TForm1.BtnExportClick(Sender: TObject);
begin
  cds.SaveToFile(EdFileName.Text, dfXML);
end;

procedure TForm1.BtnLoadClick(Sender: TObject);
begin
  cds.LoadFromFile(EdFileName.Text);
end;

Open in new window


But if you want to have the cds works on the xml directly:
Then follow above steps, then drop extra TEdit as EdField and three more TButtons (BtnInsert, BtnUpdate and BtnDelete).
3. Right click on the cds, and select Load from MyBase Table..., then locate your XML file.
4. Set the cds FileName Property to the same file you located in the previous step.
5. The three extra buttons are to manage the data (assuming you want to work on the file path field only):
procedure TForm1.BtnInsertClick(Sender: TObject);
begin
  cds.Insert;
  cds.FieldByName('FilePath').AsString:= EdField.Text;
  cds.Post;
end;

procedure TForm1.BtnUpdateClick(Sender: TObject);
begin
  cds.Edit;
  cds.FieldByName('FilePath').AsString:= EdField.Text;
  cds.Post;
end;

procedure TForm1.BtnDeleteClick(Sender: TObject);
begin
  cds.Delete;
end;

Open in new window


If you do not have xml file and you want to create one then just assign file name to the cds and create the fields manually through the Fields Editor (right click on the cds and select "Fields Editor..." and create new data fields as required).
cds-xml-sample.zip
0
 
swiss77Author Commented:
Easier than I thought.  Many thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now