0

为什么我的代码返回错误

Object reference not set to an instance of an object.treeNode.Nodes[el.Name].Nodes.Add(nodes);?

我将 xml 文件加载为树视图中的菜单,以便当我单击它时。它会将 url 加载到我的 webbrowser.. 我想在 treeview 中输出,例如:

-Home
    -About
         -MoreAbouts
         -MoreAbouts
    -About
-Index
    -About
         -MoreAbouts
         -MoreAbouts
    -About

我用这个来实现。但它返回一个错误

我的 XML 文件是

<ListOfTopics>
  <Introduction url="index.html" title="Introduction">
        <Iframe url="iframe.html" title="Iframe">
            <Iframe2 url="iframe.html" title="Iframe2"/>
            </Iframe>
    </Introduction>
  <Topic url="about.html" title="About"/>
</ListOfTopics>

C# 代码

private void XmlElementHasChild(bool index,XmlElement el, TreeView treeNode)
    {
        if (index)
        {
            if (el.HasChildNodes)
            {
                foreach (XmlElement es in el.ChildNodes)
                {
                    TreeNode nodes = new TreeNode();
                    nodes.ToolTipText = es.GetAttribute("url");
                    nodes.Name = es.GetAttribute("title");
                    nodes.Text = es.GetAttribute("title");
                    treeNode.Nodes.Add(nodes);
                    XmlElementHasChild(true,es,treeNode);
                }
            }
        }
        else
        {
            if (el.HasChildNodes)
            {

                foreach (XmlElement es in el.ChildNodes)
                {
                    TreeNode nodes = new TreeNode();
                    nodes.ToolTipText = es.GetAttribute("url");
                    nodes.Name = es.GetAttribute("title");
                    nodes.Text = es.GetAttribute("title");
                    treeNode.Nodes[el.Name].Nodes.Add(nodes);
                    XmlElementHasChild(false, es, treeNode);
               } 
            }
        }
    }
    private void TreeViewLoadXml(bool index, TreeView treeNode)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(topics);
        int i = 0;
        foreach (XmlElement el in xml.DocumentElement.ChildNodes)
        {
            TreeNode node = new TreeNode();
            node.ToolTipText = el.GetAttribute("url");
            node.Name = el.GetAttribute("title");
            node.Text = el.GetAttribute("title");

            treeNode.Nodes.Add(node);

            if (index)
            {
                XmlElementHasChild(true, el, treeNode);
            }
            else
            {
                XmlElementHasChild(false,el, treeNode);
            }
            i++;
        }

        if (index)
        {
            treeNode.Sort();
        }
    }

这是修复。对不起

private void XmlElementHasChild(bool index,XmlElement el, TreeView treeNode)
        {
                if (el.HasChildNodes)
                {
                    foreach (XmlElement es in el.ChildNodes)
                    {
                        TreeNode nodes = new TreeNode();
                        nodes.ToolTipText = es.GetAttribute("url");
                        nodes.Name = es.GetAttribute("title");
                        nodes.Text = es.GetAttribute("title");
                        treeNode.Nodes.Add(nodes);
                        XmlElementHasChild(true,es,treeNode);
                    }
                }
        }
4

0 回答 0