TXmlDocument方法也可以讀寫XML,如果你對此還不太了解,那麼今天這個例子正是你需要的,先來看如何使用TXmlDocument讀取XML:
01
procedure TForm1.LoadXmlDoc(XMLStr: string);
02
var
03
rNode: IXMLNode;
04
I: Integer;
05
begin
06
XMLDocument1.LoadFromXML(Memo1.Text);
07
rNode := XMLDocument1.ChildNodes.FindNode('UserList');
08
if rNode <> nil then begin
09
for I := 0 to rNode.ChildNodes.Count - 1 do begin
10
if rNode.ChildNodes[I].Attributes['Name'] <> null then
11
Memo2.Lines.Add('Name: ' + rNode.ChildNodes[I].Attributes['Name']);
12
if rNode.ChildNodes[I].Attributes['ID'] <> null then
13
Memo2.Lines.Add('ID: ' + rNode.ChildNodes[I].Attributes['ID']);
14
15
if rNode.ChildNodes[I].ChildNodes.FindNode('Website') <> nil then
16
Memo2.Lines.Add('WebSite: ' + rNode.ChildNodes[I].ChildNodes.FindNode('Website').Text);
17
if rNode.ChildNodes[I].ChildNodes.FindNode('Intro') <> nil then
18
Memo2.Lines.Add('Intro: ' + rNode.ChildNodes[I].ChildNodes.FindNode('Intro').Text);
19
20
memo2.Lines.Add('');
21
end;
22
end;
23
end;
TXmlDocument方法寫XML,代碼如下:
vIEw source