一个ClientDataset的Delta与XML相互转换的文章:大家都知道TClientDataSet的Delta属性保存数据集的变化,但是Delta是OleVariant类型的属性,这样如果用Delphi写Webservice,就有需要把它转成XML,特别是其他语言也要用这个Webservice的时候,因为如果Delta能和XML相互转换,Delta包的结构就是透明的了。而TClientDataSet却没有提供DeltaXML这样的属性,之前在网上找了一下,发现网上的都是把Delta赋给另一个TClientDataSet的Data,然后再通过XMLData返回XML,但这样为了一个简单的功能就得多创建一个对象实例,感觉很不好,于是研究一下TClientDataSet的源码,终于完成了Delta与XML相互转换的函数,代码如下:unit uDelta;interfaceuses DBClient,DSIntf,VarUtils,ActiveX;function DeltaToXML(Delta:OleVariant):String;function XMLToDelta(XMLData:String):OleVariant;implementationprocedure Check(FDSBase:IDSBase;Status: DBResult);varErrMsg: array[0..2048] of Char;beginif Status <> 0 thenbegin FDSBase.GetErrorString(Status, ErrMsg); raise EDBClient.Create(ErrMsg, Status);end;end;function DeltaToXML(Delta:OleVariant):String;varFDSBase: IDSBase;DataPacket: TDataPacket;VarPacket: OleVariant;beginCreateDbClientObject(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;beginDelta:=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.