An Clientdataset delta-to-XML conversion article:
As you know, TClientDataSet's Delta attribute saves changes to the dataset, but the delta is a olevariant type attribute, so if you write WebService with Delphi, you need to turn it into XML, In particular, other languages also use this webservice, because if the delta can be converted to and from XML, the structure of the delta package is transparent. And TClientDataSet did not provide deltaxml such attributes, before the online search, found that the Internet is to assign the delta to another TClientDataSet data, and then return to the XML through XMLData, But this for a simple function to create a more object instance, feel very bad, so study the source of TClientDataSet, finally completed the delta and XML conversion function, the code is as 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.
? A clientdataset Delta and XML convert to each other