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.Value := '张三'; // with xml.Root.NodeNew('item') do begin Value := '李四'; end; // xml.Root.NodeNew('item').Value := '王五'; Memo1.Text := xml.WriteToString; xml.Free; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item>张三</item> <item>李四</item> <item>王五</item> </List> *****************************************} procedure TForm1.Button2Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; xml.Root.WriteString('N1', '张三'); xml.Root.WriteString('N2', '李四'); xml.Root.WriteString('N2', '王五'); Memo1.Text := xml.WriteToString; xml.Free; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <N1>张三</N1> <N2>王五</N2> </List> *****************************************} procedure TForm1.Button3Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; xml.Root.NodeNew('item').WriteString('name', '张三'); xml.Root.NodeNew('item').WriteString('name', '李四'); xml.Root.NodeNew('item').WriteString('name', '王五'); Memo1.Text := xml.WriteToString; xml.Free; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item> <name>张三</name> </item> <item> <name>李四</name> </item> <item> <name>王五</name> </item> </List> *****************************************} procedure TForm1.Button4Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; with xml.Root.NodeNew('item') do begin WriteString('name', '张三'); WriteInteger('age', 11); end; with xml.Root.NodeNew('item') do begin WriteString('name', '李四'); WriteInteger('age', 22); //WriteFloat()、WriteDateTime()、WriteHex()、WriteBool()... end; with xml.Root.NodeNew('item') do begin WriteString('name', '王五'); WriteInteger('age', 33); end; Memo1.Text := xml.WriteToString; xml.Free; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item> <name>张三</name> <age>11</age> </item> <item> <name>李四</name> <age>22</age> </item> <item> <name>王五</name> <age>33</age> </item> </List> *****************************************} procedure TForm1.Button5Click(Sender: TObject); var xml: TNativeXml; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; with xml.Root.NodeNewAtIndex(0, 'item') do begin WriteString('name', '张三'); WriteInteger('age', 11); end; with xml.Root.NodeNewAtIndex(0, 'item') do begin WriteString('name', '李四'); WriteInteger('age', 22); end; with xml.Root.NodeNewAtIndex(0, 'item') do begin WriteString('name', '王五'); WriteInteger('age', 33); end; Memo1.Text := xml.WriteToString; xml.Free; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item> <name>王五</name> <age>33</age> </item> <item> <name>李四</name> <age>22</age> </item> <item> <name>张三</name> <age>11</age> </item> </List> *****************************************} procedure TForm1.Button6Click(Sender: TObject); var xml: TNativeXml; node: TXmlNode; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; node := xml.NodeNew('item'); node.WriteString('name', '张三'); node.WriteInteger('age', 11); xml.Root.NodeAdd(node); xml.NodeNewEx('item', node); //NodeNewEx node.WriteString('name', '李四'); node.WriteInteger('age', 22); xml.Root.NodeAdd(node); node := xml.NodeNew('item'); node.WriteString('name', '王五'); node.WriteInteger('age', 33); xml.Root.NodeAdd(node); Memo1.Text := xml.WriteToString; xml.Free; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <item> <name>张三</name> <age>11</age> </item> <item> <name>李四</name> <age>22</age> </item> <item> <name>王五</name> <age>33</age> </item> </List> *****************************************} procedure TForm1.Button7Click(Sender: TObject); var xml: TNativeXml; node: TXmlNode; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; node := xml.NodeNew('item', [xml.NodeNewText('name', '张三'), xml.NodeNewInt('age', 11)]); xml.Root.NodeAdd(node); node := xml.NodeNew('item', [xml.NodeNewText('name', '李四'), xml.NodeNewInt('age', 22)]); xml.Root.NodeAdd(node); node := xml.NodeNew('item', [xml.NodeNewText('name', '王五'), xml.NodeNewInt('age', 33)]); xml.Root.NodeAdd(node); Memo1.Text := xml.WriteToString; xml.Free; end; {结果同上} procedure TForm1.Button8Click(Sender: TObject); var xml: TNativeXml; node: TXmlNode; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; xml.Root.NodesAdd([ xml.NodeNew('item', [xml.NodeNewText('name', '张三'), xml.NodeNewInt('age', 11)]), xml.NodeNew('item', [xml.NodeNewText('name', '李四'), xml.NodeNewInt('age', 22)]), xml.NodeNew('item', [xml.NodeNewText('name', '王五'), xml.NodeNewInt('age', 33)]) ]); Memo1.Text := xml.WriteToString; xml.Free; end; {结果同上} procedure TForm1.Button9Click(Sender: TObject); var xml: TNativeXml; node: TXmlNode; begin xml := TNativeXml.CreateName('List'); xml.XmlFormat := xfReadable; node := xml.NodeNew('A', [xml.NodeNew('B', [xml.NodeNew('C', [xml.NodeNewText('D', 'E')])])]); xml.Root.NodeAdd(node); Memo1.Text := xml.WriteToString; xml.Free; end; {*************************************** <?xml version="1.0" encoding="UTF-8"?> <List> <A> <B> <C> <D>E</D> </C> </B> </A> </List> *****************************************}