unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, xmldom, XMLIntf, StdCtrls, msxmldom, XMLDoc;type TForm1 = class(TForm) XMLDocument1: TXMLDocument; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); end;var Form1: TForm1;implementation{$R *.dfm}//利用 XML 屬性建立 xml 檔案procedure TForm1.Button1Click(Sender: TObject);begin XMLDocument1.XML.Clear; XMLDocument1.XML.Add(''); XMLDocument1.XML.Add(''); XMLDocument1.XML.Add(''); XMLDocument1.XML.Add('張三姓名>'); XMLDocument1.XML.Add('男性別>'); XMLDocument1.XML.Add('34年齡>'); XMLDocument1.XML.Add('人員>'); XMLDocument1.XML.Add('科室名單>'); {查看} ShowMessage(XMLDocument1.XML.Text); {儲存} XMLDocument1.Active := True; XMLDocument1.SaveToFile('c:\temp\1.xml');end;//建立 xml 檔案的標準方法procedure TForm1.Button2Click(Sender: TObject);var pNode,cNode: IXMLNode; {定義兩個節點: 父節點、子節點}begin XMLDocument1.XML.Clear; XMLDocument1.Active := True; {必須先啟用} XMLDocument1.Version := '1.0'; {設定版本} XMLDocument1.Encoding := 'GB2312'; {設定語言} pNode := XMLDocument1.AddChild('科室名單'); {添加的第一個節點是根節點, 現在的 pNode 是根節點} pNode.SetAttribute('備忘', '測試'); {為根節點設定屬性} pNode := pNode.AddChild('人員'); {為根節點添加子節點, 現在的 pNode 是 "人員" 節點} pNode.SetAttribute('職務', '科長'); {設定屬性} pNode.SetAttribute('備忘', '正局級'); cNode := pNode.AddChild('姓名'); {為 pNode 添加子節點, 傳回值 cNode 指向了新添加的節點} cNode.Text := '張三'; cNode := pNode.AddChild('性別'); cNode.Text := '男'; cNode := pNode.AddChild('年齡'); cNode.Text := '34'; {查看} ShowMessage(XMLDocument1.XML.Text); {儲存} XMLDocument1.SaveToFile('c:\temp\2.xml');end;end.