简体   繁体   中英

C# Treeview checking if node exists

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 XML script. I can get data from the XML document no problem. My question is when looping through the XmlDocument and adding nodes to the root node, I want to ensure that if a department is already within the treeview then it is not added again. I should also add that each Department also has a list of scripts that need to be child nodes of the department.

My code so far is:

        XmlDocument xDoc = new XmlDocument();
        xDoc.LoadXml(scriptInformation);
        TreeNode t1;
        TreeNode rootNode = new TreeNode("Script View");
        treeView1.Nodes.Add(rootNode);
        foreach (XmlNode node in xDoc.SelectNodes("//row"))
        {
            t1 = new TreeNode(node["DEPARTMENT"].InnerXml);
           //How to check if node already exists in treeview?



        }

Thanks.

if(treeView1.Nodes.ContainsKey("DEPARTMENT")){
//...
}

EDIT: Recursive method:

 bool exists = false;
        foreach (TreeNode node in treeView1.Nodes) {
            if (NodeExists(node, "DEPARTMENT"))
                exists = true;
        }
    private bool NodeExists(TreeNode node, string key) {
        foreach (TreeNode subNode in node.Nodes) {
            if (subNode.Text == key) {
                return true;
            }
            if (node.Nodes.Count > 0) {
                NodeExists(node, key);
            }
        }
        return false;
    }

Depending upon the size of your XML file, you could consider using an associated List for fast lookup. As you add each node to the TreeView also add it to the List .

You can do something like this:

TreeNode parentNode = t1.Parent;
if (parentNode != null}
{
    if(parentNode.Nodes.Cast<TreeNode>().ToList().Find(t => t.Text.Equals(node["DEPARTMENT"].InnerXml) == null)
    {
        //Add node
    }
}
else
{
    bool isFound = true;
    if (treeView1.Nodes.Cast<TreeNode>().ToList().Find(t => t.Text.Equals(node["DEPARTMENT"].InnerXml) == null)
    {
        isFound = false;
    }

    if(!isFound)
    {
        //Add node
    }
}

If your XML document has a set structure where 'Departments' will always be indexed at 1;

ie:

index:[0] Scripts
    index:[1] Department
        index:[2] Script
    index:[1] Department2
        index:[2] Script

Then you could encapsulate the following code into a method where 'name' is a string parameter and the return type is boolean.

foreach (TreeNode node in uxTreeView.Nodes[0].Nodes) {
    if (node.Name.ToLower() == name.ToLower()) {
        return true;
    }
}
return false;

The idea is you would call that function each time you encounter a 'Department' node in your Xml, before creating the TreeNode.

Full example:

private bool DepartmentNodeExists(string name) {
    foreach (TreeNode node in uxTreeView.Nodes[0].Nodes) {
        if (node.Name.ToLower() == name.ToLower()) {
            return true;
        }
    }
    return false;
}

Lastly, the easy way:

private bool DepartmentNodeExists(string name) {
    if (uxTreeView.Nodes[0].ContainsKey(name)) {
        return true;
    }
    else {
        return false;
    }
}

These are all just refactored and encapsulated into their own named methods, you of course could just call:

if (uxTreeView.Nodes[0].ContainsKey(name)) {
    // do not create TreeNode
}

...during your parsing of your XML. PS. These examples all assume that you have the first root node in the TreeView already created and added to the TreeView.

Not sure about the document structure...
Couldn't you use Linq to Xml, load the document and get the distinct row ( row = department?) and consider only those elements to create a TreeNode? It is more efficient than trying to find if a node with such a text has already been added. ex:

 var rows =      (  from row  in XDocument.Load(document).Root.Elements("row")
                    select row
                 ).Distinct(new SampleElementComparerOnNameAttribute());

Here the EqualityComparer is made on the "name" attribute value assuming the doc structure to be

<rows><row name='dep1'><script>script1</script><script>script2</script></row><row name='dep1'><script>script3</script><script>script4</script></row></rows>

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].

It depends on the structure of your input. Since you don't show how exactly you add your subnodes I can only point you towards either the Contains or the ContainsKey method of the Nodes property, either of the treeView1 itself, or of any subnodes you add. You should use an overload of the Add method to specify a key name to simplify lookup.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM