问题来源: http://www.cnblogs.com/del/archive/2008/06/12/1114450.html#1223758
本例效果图:
代码文件:
本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, DBClient; type TForm1 = class(TForm) ClientDataSet1: TClientDataSet; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses XMLDoc; {为了使用 FormatXMLData 函数} procedure TForm1.Button1Click(Sender: TObject); begin ClientDataSet1.FieldDefs.Clear; with ClientDataSet1.FieldDefs.AddFieldDef do begin Name := 'Name'; DataType := ftString; Size := 10; end; with ClientDataSet1.FieldDefs.AddFieldDef do begin Name := 'Age'; DataType := ftInteger; end; ClientDataSet1.CreateDataSet; // ClientDataSet1.LogChanges := False; ClientDataSet1.Open; end; procedure TForm1.Button2Click(Sender: TObject); begin ClientDataSet1.Append; ClientDataSet1.Fields[0].Value := '张三'; ClientDataSet1.Fields[1].Value := 11; ClientDataSet1.Append; ClientDataSet1.Fields[0].Value := '李四'; ClientDataSet1.Fields[1].Value := 22; end; procedure TForm1.Button3Click(Sender: TObject); begin Memo1.Clear; Memo1.Text := FormatXMLData(ClientDataSet1.XMLData); end; procedure TForm1.Button4Click(Sender: TObject); begin ClientDataSet1.SaveToFile('c:\temp\MyXml.xml', dfXML); {如果要存得格式漂亮就用下面这句:} Memo1.Lines.SaveToFile('c:\temp\MyXml2.xml'); end; end.窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = #20381#27425#28857#20987#25353#38062 ClientHeight = 211 ClientWidth = 445 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False Position = poDesktopCenter PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 358 Top = 21 Width = 75 Height = 25 Caption = #21019#24314 TabOrder = 0 OnClick = Button1Click end object Button2: TButton Left = 358 Top = 64 Width = 75 Height = 25 Caption = #28155#21152 TabOrder = 1 OnClick = Button2Click end object Button3: TButton Left = 358 Top = 108 Width = 75 Height = 25 Caption = #26597#30475 TabOrder = 2 OnClick = Button3Click end object Button4: TButton Left = 358 Top = 152 Width = 75 Height = 25 Caption = #20445#23384 TabOrder = 3 OnClick = Button4Click end object Memo1: TMemo Left = 0 Top = 0 Width = 345 Height = 211 Align = alLeft Lines.Strings = ( 'Memo1') ScrollBars = ssBoth TabOrder = 4 end object ClientDataSet1: TClientDataSet Aggregates = <> Params = <> Left = 168 Top = 8 end end打开一看, 发现程序很不严谨, 但能说明问题了, 不在修改了.