uses NativeXML;
procedure TForm1.Button1Click(Sender: TObject);
var
xml: TNativeXML;
node: TXMLNode;
begin
xml := TNativeXML.CreateName('List');
xml.XMLFormat := xfReadable;
node := XML.Root.NodeNew('item');
node.AttributeAdd('ID', '1');
node.AttributeAdd('age', '11');
node.Value := '張三';
with XML.Root.NodeNew('item') do
begin
AttributeAdd('ID', '2');
AttributeAdd('age', '22');
Value := '李四';
end;
with XML.Root.NodeNew('item') do
begin
AttributeAdd('ID', '3');
AttributeAdd('age', '33');
Value := '王五';
end;
Memo1.Text := XML.WriteToString;
XML.Free;
end;
{***************************************
<?XML version="1.0" encoding="UTF-8"?>
<List>
<item ID="1" age="11">張三</item>
<item ID="2" age="22">李四</item>
<item ID="3" age="33">王五</item>
</List>
*****************************************}
procedure TForm1.Button2Click(Sender: TObject);
var
xml: TNativeXML;
node: TXMLNode;
attr1,attr2: TsdAttribute;
begin
xml := TNativeXML.CreateName('List');
xml.XMLFormat := xfReadable;
attr1 := TsdAttribute.Create(XML);
attr1.Name := 'ID';
attr1.Value := '1';
attr2 := TsdAttribute.Create(XML);
attr2.Name := 'age';
attr2.ValueAsInteger := 11;
node := XML.Root.NodeNew('item');
node.AttributeAdd(attr1);
node.AttributeAdd(attr2);
node.Value := '張三';
attr1 := TsdAttribute.CreateName(XML, 'ID');
attr1.Value := '2';
attr2 := TsdAttribute.CreateNameValue(XML, 'age', '22');
node := XML.Root.NodeNew('item');
node.AttributesAdd([attr1, attr2]);
node.Value := '李四';
node := XML.Root.NodeNew('item');
node.AttributesAdd([TsdAttribute.CreateNameValue(xml, 'ID', '3'), TsdAttribute.CreateNameValue(XML, 'age', '33')]);
node.Value := '王五';
Memo1.Text := XML.WriteToString;
XML.Free;
end;
{結果同上}
procedure TForm1.Button3Click(Sender: TObject);
var
xml: TNativeXML;
node: TXMLNode;
attr1,attr2: TsdAttribute;
begin
xml := TNativeXML.CreateName('List');
xml.XMLFormat := xfReadable;
attr1 := XML.AttrText('ID', '1');
attr2 := XML.AttrInt('age', 11);
node := XML.Root.NodeNew('item');
node.AttributesAdd([attr1, attr2]);
node.Value := '張三';
node := XML.Root.NodeNew('item');
node.AttributesAdd([xml.AttrText('ID', '2'), XML.AttrInt('age', 22)]);
node.Value := '李四';
with XML.Root.NodeNew('item') do begin
AttributesAdd([xml.AttrText('ID', '3'), XML.AttrInt('age', 33)]);
Value := '王五';
end;
Memo1.Text := XML.WriteToString;
XML.Free;
end;
{結果同上}
procedure TForm1.Button4Click(Sender: TObject);
var
xml: TNativeXML;
node: TXMLNode;
begin
xml := TNativeXML.CreateName('List');
xml.XMLFormat := xfReadable;
node := xml.NodeNewTextAttr('item', '張三', [xml.AttrText('ID', '1'), XML.AttrInt('age', 11)]);
XML.Root.NodeAdd(node);
node := xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), XML.AttrInt('age', 22)]);
XML.Root.NodeAdd(node);
node := xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), XML.AttrInt('age', 33)]);
XML.Root.NodeAdd(node);
Memo1.Text := XML.WriteToString;
XML.Free;
end;
{結果同上}
procedure TForm1.Button5Click(Sender: TObject);
var
xml: TNativeXML;
begin
xml := TNativeXML.CreateName('List');
xml.XMLFormat := xfReadable;
XML.Root.NodesAdd([
xml.NodeNewTextAttr('item', '張三', [xml.AttrText('ID', '1'), XML.AttrInt('age', 11)]),
xml.NodeNewTextAttr('item', '李四', [xml.AttrText('ID', '2'), XML.AttrInt('age', 22)]),
xml.NodeNewTextAttr('item', '王五', [xml.AttrText('ID', '3'), XML.AttrInt('age', 33)])
]);
Memo1.Text := XML.WriteToString;
XML.Free;
end;
{結果同上}
procedure TForm1.Button6Click(Sender: TObject);
var
xml: TNativeXML;
node: TXMLNode;
begin
xml := TNativeXML.CreateName('List');
xml.XMLFormat := xfReadable;
node := xml.NodeNewAttr('item', [xml.AttrText('name', '張三'), XML.AttrInt('age', 11)]);
XML.Root.NodeAdd(node);
node := xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), XML.AttrInt('age', 22)]);
XML.Root.NodeAdd(node);
node := xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), XML.AttrInt('age', 33)]);
XML.Root.NodeAdd(node);
Memo1.Text := XML.WriteToString;
XML.Free;
end;
{***************************************
<?XML version="1.0" encoding="UTF-8"?>
<List>
<item name="張三" age="11"/>
<item name="李四" age="22"/>
<item name="王五" age="33"/>
</List>
*****************************************}
procedure TForm1.Button7Click(Sender: TObject);
var
xml: TNativeXML;
node: TXMLNode;
begin
xml := TNativeXML.CreateName('List');
xml.XMLFormat := xfReadable;
XML.Root.NodesAdd([
xml.NodeNewAttr('item', [xml.AttrText('name', '張三'), XML.AttrInt('age', 11)]),
xml.NodeNewAttr('item', [xml.AttrText('name', '李四'), XML.AttrInt('age', 22)]),
xml.NodeNewAttr('item', [xml.AttrText('name', '王五'), XML.AttrInt('age', 33)])
]);
Memo1.Text := XML.WriteToString;
XML.Free;
end;
{結果同上}