1. Save the Treeview structure as an XML file Function
Procedure tmainform. tree2xml (tree: ttreeview );
VaR
TN: ttreenode;
Xmldoc: txmldocument;
Inode: ixmlnode;
Procedure processtreeitem (TN: ttreenode; inode: ixmlnode );
VaR
CNode: ixmlnode;
Begin
If Tn = nil then exit;
CNode: = inode. addchild ('item ');
CNode. attributes ['text']: = tn. text;
CNode. attributes ['imageindex']: = tn. imageindex;
CNode. attributes ['selectedindex ']: = tn. selectedindex;
// Child Nodes
TN: = tn. getfirstchild;
Whiletn <> nil do
Begin
Processtreeitem (TN, cNode );
TN: = tn. getnextsibling;
End;
End; (* processtreeitem *)
Begin
Xmldoc: = txmldocument. Create (NiL );
Xmldoc. Active: = true;
Inode: = xmldoc. addchild ('tree2xml ');
Inode. attributes ['app']: = paramstr (0 );
TN: = tree. topitem;
While tn <> nil do
Begin
Processtreeitem (TN, inode );
TN: = tn. getnextsibling;
End;
Xmldoc. encoding: = 'utf-8 ';
Xmldoc. Version: = '1. 0 ';
Xmldoc. savetofile (changefileext (paramstr (0), '. xml '));
Xmldoc: = nil;
End;
Ii. Generate a Treeview from XML
Procedure tmainform. xml2tree (tree: ttreeview; xmldoc: txmldocument );
VaR
Inode: ixmlnode;
Procedure processnode (node: ixmlnode; TN: ttreenode );
VaR
CNode: ixmlnode;
Begin
If node = nil then exit;
With node do
Begin
TN: = tree. Items. addchild (TN, attributes ['text']);
Tn. imageindex: = INTEGER (attributes ['imageindex']);
Tn. selectedindex: = INTEGER (attributes ['selectedindex ']);
End;
CNode: = node. childnodes. first;
While cNode <> nil do
Begin
Processnode (cNode, TN );
CNode: = cNode. nextsibling;
End;
End; (* processnode *)
Begin
Tree. Items. Clear;
Xmldoc. filename: = changefileext (paramstr (0), '. xml ');
Xmldoc. Active: = true;
Inode: = xmldoc. documentelement. childnodes. first;
While inode <> nil do
Begin
Processnode (inode, nil );
Inode: = inode. nextsibling;
End;
Xmldoc. Active: = false;
End;