xmldelphitxmldocument

Adding the same IXMLNode multiiple times in a TXMLDocument


I've noticed that it is possible to add the same node multiple times into the same TXMLDocument - is this a good idea?

  n := XMLDocument1.CreateElement('Test', '');
  XMLDocument1.DocumentElement.ChildNodes.Add(n);
  XMLDocument1.DocumentElement.ChildNodes.Add(n);
  n.Text := 'fool me once...';

The node test will appear multiple times in the tree

Edit: To make my question clear - Will it cause any problems if I add an IXMLNode multiple times in a TXMLDocument?


Solution

  • By adding node to a document you won't lose a reference to just added node, so there's nothing wrong to do it several times. If it's a good idea is then just a design question - if you need to add several same nodes to your document at one time, then it's the best way to do so.