// --------------------------------------------------------- //
// Name: XMLFromDataSet(Source: TDataSet): String;
// Description: Transfer the specified dataset into the XML
// document which self-definition;
// Input Para: 1) Source: Dataset -- Original dataset
// Output Par: 1) String -- XML document's string;
// Comment: 1) 把数据集转换为自定义的XML格式;
// --------------------------------------------------------- //
function XMLFromDataSet(Source: TDataSet): String;
var
FieldIndex: Integer;
slst: TStringList;
begin
result := '';
slst := TStringList.Create;
try
// Simple XML header
slst.Add('<?xml version = "1.0" ?>');
// Generic name for set of all records
slst.Add('<RecordSet tablename="' + Source.Name + '">');
while not Source.EOF do
begin
// Generic tag identifying new record
slst.Add('<Record>');
// Loop through each of the Fields in the DataSet
for FieldIndex := 0 to Source.FieldCount-1 do
// Generate the XML of the data
slst.Add('<' + Source.Fields[FieldIndex].FieldName + '>' + Source.Fields[FieldIndex].AsString + '</' + Source.Fields[FieldIndex].FieldName + '>');
// Closing record tag
slst.Add('</Record>');
// Go to the next record
Source.Next;
end; // while
slst.Add('</RecordSet>');
// return the result
result := slst.Text;
finally
slst.free;
end;
end;
// Data
slst.Add('<rs:data>');
slst.Add('<rs:insert>');
while not Source.EOF do
begin
// Generic tag identifying new record
slst.Add('<z:row ');
// Loop through each of the Fields in the DataSet
for FieldIndex := 0 to Source.FieldCount-1 do
// Add the Param for the Field
slst.Add(Source.Fields[FieldIndex].FieldName + '="' + Source.Fields[FieldIndex].AsString + '" ');
// Close record tag
slst.Add(' />');
// Go to the next record
Source.Next;
end; // while