0

我有一个 Treeview,在选择节点时,属性和值必须显示在列表框中。
在treeView1_AfterSelect中,文本解析代码依赖于树视图中某个节点的文本表示,可以随时更改,打破了列表显示的整个逻辑。树视图和列表显示之间的这种强依赖关系应该通过将树视图中的节点绑定到 XML 文档的节点来消除,以便可以使用原始 Xml 数据在列表中显示文本。我应该在这里写什么?

private static void AddingNodesToTree(XmlNode xmlNode,TreeNode tnode)  
        {  
            //Adding nodes to tree while looping through the entire XML file  
            if (xmlNode.HasChildNodes)  
            {  
                XmlNodeList nodeList = xmlNode.ChildNodes;                 
                for (int i = 0; i <= nodeList.Count - 1; i++)  
                {  
                    XmlNode xmladdtreeNode = xmlNode.ChildNodes[i];  
                    String nodetype = "" + xmladdtreeNode.NodeType;  
                    if (nodetype.Equals("Text") || nodetype.Equals("Comment"))    
                    {  
                    tnode.Nodes.Add(new TreeNode(xmladdtreeNode.InnerText));           
                    }  
                    else                                                    
                    {  
                        String name = "<" + xmladdtreeNode.Name;  
                        XmlAttributeCollection attCol = xmladdtreeNode.Attributes; 
                        foreach (XmlAttribute xmlatt in attCol)  
                        {  
                        name += " " + xmlatt.Name + "=\"" + xmlatt.Value + "\"";  
                        }  
                        name +=  ">";  
                        TreeNode tn = new TreeNode(xmladdtreeNode.Name);  
                        tn.Text = name;  
                        tnode.Nodes.Add(tn);  
                        TreeNode treeNode = tnode.Nodes[i];  
                        AddingNodesToTree(xmladdtreeNode,treeNode);   
                        }  
                }//for  
            }//if  
          else 
            {  
                tnode.Text = xmlNode.OuterXml.Trim();
            }//else   
        }//AddingNodesToTree  
        //To show Attributes and values of selected Nodes.  
        private void treeView1_AfterSelect(object sender,TreeViewEventArgs e)  
        {  
            TreeNode treenode = e.Node;   //Node selected in Treeview            
            String text = treenode.Text;  
            String relevent = text;  
            Boolean flag = true;  
            while (flag)  
            {  
                int SpaceIndex = relevent.IndexOf(" ");  
                if (SpaceIndex != -1)  
                {  
                    int indexofEqual = relevent.IndexOf('=', SpaceIndex);  
                    if (indexofEqual != -1)  
                    {  
                        int indexOFValue = relevent.IndexOf("\"", indexofEqual + 2);  
                        if (indexOFValue != -1)  
                        {  
  String attribute = relevent.Substring(SpaceIndex + 1, indexofEqual - SpaceIndex - 1);   
  String value = relevent.Substring(indexofEqual + 2, indexOFValue - indexofEqual - 2);   
  listBox1.Items.Add("Attribute : " + attribute + "   Value : " + value);                 
                            relevent = relevent.Substring(indexOFValue);
                        }
                        else
                        {
                         listBox1.Items.Add("Bad format of the xml file for this node");
                         flag = false;
                        }
                    }
                    else
                    {
                        flag = false;
                    }
                }
                else
                {
                    flag = false;
                }
            }
        }//AfterSelect()

谢谢....

4

2 回答 2

0

如果我正确理解了这个问题,您希望在选择 TreeNode 时返回 XmlNode。通常的解决方案是将 XmlNode 存储在 Tag 属性中:

TreeNode tn = new TreeNode();  
tn.Text = name;  
tn.Tag = xmladdtreeNode;

并在 AfterSelect

TreeNode treenode = e.Node; 
XmlNode xmlNode = (XmlNode) treeNode.Tag;
于 2009-03-02T21:13:30.333 回答
0

我尝试了这个,但在 attcol 上的这一行 foreach (XmlAttribute xmlatt in attCol)得到 了 NullRefrenceException

这是我写的代码..

private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {
        listBox1.Items.Clear();
        XmlNode xNode = e.Node.Tag as XmlNode;
        XmlAttributeCollection attCol = xNode.Attributes; 
        foreach (XmlAttribute xmlatt in attCol)
        {
            listBox1.Items.Add(xmlatt.Name);
            listBox1.Items.Add(xmlatt.Value);

        }
    } //AfterSelect()
于 2009-03-03T13:34:54.997 回答