加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

用 TClientDataSet 创建 XML 文件的过程 - 回复"丁永其"

发布时间:2020-12-15 22:23:22 所属栏目:百科 来源:网络整理
导读:问题来源: http://www.cnblogs.com/del/archive/2008/06/12/1114450.html#1223758 本例效果图: 代码文件: unit Unit1;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,DB,DBClient;type TForm1 = class(
问题来源: 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:tempMyXml.xml',dfXML);

  {如果要存得格式漂亮就用下面这句:}
  Memo1.Lines.SaveToFile('c:tempMyXml2.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
打开一看,发现程序很不严谨,但能说明问题了,不在修改了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读