? A clientdataset Delta and XML convert to each other

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.