一個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;
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.
原文出處:
http://hi.baidu.com/0xcea4/item/27f77f338b1abf81c2cf2928
函數