如何通过将树视图中的节点绑定到 XML 文档的节点来显示树节点

发布于 2024-07-14 13:20:43 字数 3674 浏览 9 评论 0原文

我有一个树视图,在选择节点时,属性和值必须显示在列表框中。
在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()

谢谢....

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-07-21 13:20:43

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

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

以及 AfterSelect 中

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

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;
昔梦 2024-07-21 13:20:43

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

这是我编写的代码..

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文