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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:
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
Using_XML_in_Database_Applications (Emb...)

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


Open in new window

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);
  cds.SaveToFile(EdFileName.Text, dfXML);

procedure TForm1.BtnLoadClick(Sender: TObject);

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);
  cds.FieldByName('FilePath').AsString:= EdField.Text;

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

procedure TForm1.BtnDeleteClick(Sender: TObject);

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).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
swiss77Author Commented:
Easier than I thought.  Many thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.