问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/3987486/reading-and-writing-into-the-xml-file