How to display treenodes by binding nodes in the treeview to nodes of the XML document

柔情痞子 提交于 2019-12-13 03:18:06

问题


I have a Treeview where on selecting a node the attributes and values has to be displayed in listbox.
In treeView1_AfterSelect, the text parsing code depends on the textual representation for a node in the tree view, which can be changed at any time and break the entire logic of list display. This strong dependency between the tree view and the list display should be eliminated by binding nodes in the treeview to nodes of the XML document, so that the raw Xml data can be used to display text in the list.What should i write here?

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()

Thanks....


回答1:


If I understand the question correctly you want to get back to the XmlNode when a TreeNode is selected. The usual solution is to store the XmlNode in the Tag property:

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

and in the AfterSelect

TreeNode treenode = e.Node; 
XmlNode xmlNode = (XmlNode) treeNode.Tag;



回答2:


i tried this but got the NullRefrenceException at this line foreach (XmlAttribute xmlatt in attCol) on attcol

This is the code i have written..

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()


来源:https://stackoverflow.com/questions/602495/how-to-display-treenodes-by-binding-nodes-in-the-treeview-to-nodes-of-the-xml-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!