C# TreeView 搜索功能实现

C# TreeView search function implementation

我目前正在尝试在我正在进行的一个小项目中实现搜索功能 on.A 关于该项目的一些背景知识,我有一个 xml 从那里导入一些存储在treeview.My 目标是能够给出一个字符串并在树视图中仅显示包含该特定字符串的节点。

我的树视图下面的 xml 看起来像这样

 Breakfast
     Belgian waffles
     Strawberry Belgian Waffles 

如果我 select 一个节点,我有一个带有一些文本框的组框,其中包含每个项目的属性(价格、卡路里等)

我希望我的功能能够搜索任何东西..即如果我给出价格,则在树视图中以该价格显示 item/items,如果我搜索华夫饼以显示所有华夫饼.

<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>.95</price>
    <description>
      Two of our famous Belgian Waffles with plenty of real maple syrup
    </description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>.95</price>
    <description>
      Light Belgian waffles covered with strawberries and whipped cream
    </description>
    <calories>900</calories>
  </food>
</breakfast_menu>

我有一个递归函数,但是在第 3(TreeNode t = treeNode.FirstNode) 行它弹出一个空引用 exception.My 猜测是当它命中一个没有子节点但没有子节点时弹出这个异常我现在不知道怎么处理。

private void IterateRecursive(TreeNode treeNode)
        {
            TreeNode t = treeNode.FirstNode;
            if (t!=null)
            {
                foreach (TreeNode tn in treeNode.Nodes)
                {
                    IterateRecursive(tn);
                }
            }
            if (!treeNode.Text.Contains(textBox_search_string.ToString()))
            {
                treeView1.Nodes.Remove(treeNode);
            }
        }

此外,如果有其他方法可以实现此搜索功能,我愿意接受建议。

     public static void Search(string s)
        {
          bool node_add_decision = false;
          int i = 0;
          foreach (XmlNode p_node in xml_list)
          {
                i++;
                node_add_decision = false;
                if (p_node.Attributes.Count > 0)
                {
                    foreach (XmlAttribute attr in p_node.Attributes)
                    {
                        if (attr.Name.Contains(s) || attr.Value.Contains(s))
                        {
                            node_add_decision = true;
                        }
                    }                  
                    if (!node_add_decision)
                    {
                        node_add_decision=search_c(p_node, s);
                    }
                }
                if (node_add_decision)
                {
                    Add_search_list(p_node,i);
                }
             }
         }

  private static bool search_c(XmlNode xn, string s)
    {
      if (xn.HasChildNodes)
            {
                foreach (XmlNode chld in xn.ChildNodes)
                {
                        for (int i = 0; i <= chld.ChildNodes.Count - 1; i++)
                        {
                            if(chld.ChildNodes[i].Value!=null)
                            {
                                if (chld.ChildNodes[i].Value.Contains(s))
                                    return true;
                            }
                            if (chld.ChildNodes[i].Name.Contains(s))
                            {
                                return true;
                            }
                            else {
                                if (chld.ChildNodes[i].Attributes.Count > 0)
                                {
                                    foreach (XmlAttribute attr in chld.ChildNodes[i].Attributes)
                                    {
                                        if (attr.Name.Contains(s) || attr.Value.Contains(s))
                                        {
                                            return true;
                                        }
                                    }
                                }                            
                        }
                    }
                }
            }
            return false;
        }

所以,我得到了名为 Search 的主要函数,对于列表中的每个节点,它将在所有节点中搜索所需的字符串;找到 attributes.If,而不是 node_add_decision设置为true,意味着该节点包含该字符串,不再需要搜索它 childs.If 在当前节点中没有该字符串的踪迹,该节点决定将为false,意味着我们必须搜索它childs.

search_c 是通过 childs.For 搜索字符串的每个 child 节点搜索字符串的函数,如果找到 return 则为真,否则为假。

对于我的情况,我不得不显示它不包含字符串的事件,但是它的 child does.If 这不是你的情况,你可以放弃 search_c 函数和代码的节点决策部分。