We all know that the Delta attribute of tclientdataset stores changes in the dataset, but Delta is an olevariant attribute. If you use Delphi to write a WebService, You need to convert it into XML, especially when other languages use this WebService, the structure of the delta package is transparent if Delta can be converted to XML. However, tclientdataset does not provide deltaxml attributes. I searched online and found that Delta is assigned to the data of another tclientdataset, and then XML is returned through xmldata, however, it is difficult to create an object instance for a simple function. So I studied the source code of tclientdataset and finally completed the conversion between delta and XML,CodeAs follows:
Unit udelta;
Interface
Uses dbclient, dsintf, varutils, ActiveX;
Function deltatoxml (delta: olevariant): string;
Function xmltodelta (xmldata: string): olevariant;
Implementation
Procedure check (fdsbase: idsbase; Status: dbresult );
VaR
Errmsg: array [0 .. 2048] of char;
Begin
If status <> 0 then
Begin
Fdsbase. geterrorstring (status, errmsg );
Raise edbclient. Create (errmsg, status );
End;
End;
function deltatoxml (delta: olevariant): string;
var
fdsbase: idsbase;
datapacket: tdatapacket;
varpacket: olevariant;
begin
createdbclientobject (clsid_dsbase, idsbase, fdsbase);
safearraycheck (ActiveX. safearraycopy (vartodatapacket (DELTA), datapacket);
check (fdsbase, fdsbase. appenddata (datapacket, true);
fdsbase. setprop (dspropxml_streammode, xmlon);
check (fdsbase, fdsbase. streamds (datapacket);
datapackettovariant (datapacket, varpacket);
result: = variantarraytostring (varpacket);
// freedatapacket (datapacket );
end;
Function xmltodelta (xmldata: string): olevariant;
VaR delta: olevariant;
Datapacket: tdatapacket;
Fdsbase: idsbase;
Begin
Delta: = stringtovariantarray (xmldata );
Safearraycheck (safearraycopy (vartodatapacket (DELTA), datapacket ));
Createdbclientobject (clsid_dsbase, idsbase, fdsbase );
Check (fdsbase, fdsbase. appenddata (datapacket, true ));
Fdsbase. setprop (dspropxml_streammode, xmloff );
Check (fdsbase, fdsbase. streamds (datapacket ));
Datapackettovariant (datapacket, result );
// Freedatapacket (datapacket );
End;
End.