C# Treeview checking if node exists

前端 未结 8 747
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 12:42

I\'m trying to populate a treeview from an XmlDocument. The Root of the tree is set as \'Scripts\' and from the root the next level should be \'Departments\' which is within the

8条回答
  •  不要未来只要你来
    2021-01-23 13:12

    I use,

    string department = node["DEPARTMENT"].InnerXml;
    TreeNode node = parentNode.Nodes[department] ?? parentNode.Nodes.Add(department, department);
    

    That line guarantees that a lookup of the value department will be done first, if not found it creates it. You have to do the double entry in Add() so it will have a key value you can do the lookup with the .Nodes[department].

提交回复
热议问题