1

I need to work on XML files using Delphi.

I want to present the xml data in a DBGrid to the user and save the changes done by user in the XML file.

For example in the below xml (which was presented to the user), if user changed City of ABC under client and added a new customer with NickName as "AAA" those changes should be reflected in the XML file.

<Data LinkID=”0”>
  <Client>
      <Item Name=”ABC” Mobile=”1234” City=”IN” />
      <Item Name=”PQR” Mobile=”5678” City=”IN” />
  </Client>
  <Customer>
    <Item NickName=”XYZ” Phone=”1254” City=”IN” />
    <Item NickName=”MNO” Phone =”41255” City=”IN” />
  </Customer>
</Data>

I am working with XMLDocument and ClientDataSet to achieve this but without success.

Can anyone help me in achieving this?

Bharat
  • 6,828
  • 5
  • 35
  • 56
  • 2
    Bharat, check this question to see how save a `TClientDataset` to a XML file http://stackoverflow.com/questions/3848853/using-delphi7-tclientdataset-is-it-possible-to-have-it-save-its-xml-contents-in/3848892#3848892 – RRUZ Oct 21 '10 at 12:57
  • Can you elaborate on which problems or errors are you having? Can you please also add the code you try to use? – Guillem Vicens Oct 21 '10 at 12:57
  • @Vicens: I am getting "Mismatch in datapacket" error – Bharat Oct 21 '10 at 13:10
  • as far as I know that error may come from a type mismatch in some field. It is hard to say without seeing the code, though. – Guillem Vicens Oct 21 '10 at 14:43

1 Answers1

4

The problems lies with the XML file i am using.

The XML file should be in a specified format which must have <METADATA> and <ROWDATA> tags.

I changed the xml to that format.

<?xml version="1.0" standalone="yes"?>  
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD attrname="Name" fieldtype="string" WIDTH="50"/>
<FIELD attrname="Mobile" fieldtype="string" WIDTH="20"/>
<FIELD attrname="City" fieldtype="string" WIDTH="20"/>
</FIELDS><PARAMS CHANGE_LOG="6 1 8"/>
</METADATA>
<ROWDATA>
<ROW Name="ABC" Mobile="1234" City="IN"/>
<ROW Name="PQR" Mobile="5678" City="IN"/>
<ROW Name="AAA" Mobile="7894" City="IN"/>
<ROW Name="MNO" Mobile="4569" City="IN"/>
<ROW Name="ABC" Mobile="45685" City="IN"/>
</ROWDATA>
</DATAPACKET>

Next i used ClientDataSet1.LoadFromFile('E:\projects\XML\Sample App with CDS\XmlText.xml'); to read the XML file.

After modifications were done in the grid i used ClientDataSet1.SaveToFile('E:\projects\XML\Sample App with CDS\XmlText.xml',dfXML); method to save back to the XML.

There is no need of XMLDocument for this purpose.

Bharat
  • 6,828
  • 5
  • 35
  • 56