pRow := aNode.ChildNodes[j];
For i := 0 To FPropList.PropCount - 1 Do
Begin
pInfo := FPropList.Props[i];
If ( pInfo^.PropType^.Kind In DefaultFilter ) Then
SetVariant( i,
String( pRow.ChildNodes[WideString( pInfo^.Name )].Text ) );
End;
EndEdit;
End;
FDataSet.First;
end;
procedure TMDataSetProxy.SaveToXML(aNode: IXMLNode);
Var
i : Integer;
pInfo : PPropInfo;
pRow : IXMLNode;
begin
While ForEach Do
Begin
pRow := aNode.AddChild( ''Row'' );
For i := 0 To FPropList.PropCount - 1 Do
Begin
pInfo := FPropList.Props[i];
If ( pInfo^.PropType^.Kind In DefaultFilter ) Then
pRow.AddChild( WideString( pInfo^.Name ) ).Text
:= GetVariant( i );
End;
End;
end;
下面是一个简单的DEMO,其中包括了对数据集的XML持久化。注意Load的时候Employee成员连接的是ADODataSet2,它连接到一个包含了这几个字段的表,各字段类型与Employee表相同,但内容为空,并且去掉了EmployeeID的Identity。Load完成后,Employee表中这几个字段的内容将被复制到此表中。
TDemoCompany = class( TPersistent )
private
FEmployee : TDSPEmployee;
FCompany : String;
FCode : Integer;
published
property Employee : TDSPEmployee Read FEmployee Write FEmployee;
property Company : String Read FCompany Write FCompany;
Property Code : Integer Read FCode Write FCode;
End;
procedure TForm1.SaveClick(Sender: TObject);
Var
demo : TDemoCompany;
begin
demo := TDemoCompany.Create;
demo.Employee := TDSPEmployee.Create( ADODataSet1 );
demo.Company := ''Demo company'';
demo.Code := 987654;
Try
XMLDocument1.Active := true;
TMXMLPersistent.SaveObjToXML( XMLDocument1.AddChild( ''Demo'' ), demo );
XMLDocument1.SaveToFile( ''temp.xml'' );
XMLDocument1.Active := false;
Finally
demo.Employee.Free;
demo.Employee := Nil;
demo.Free;
End;
end;
procedure TForm1.LoadClick(Sender: TObject);
Var
demo : TDemoCompany;
begin
demo := TDemoCompany.Create;
demo.Employee := TDSPEmployee.Create( ADODataSet2 );
Try