树视图中的XML节点列表

时间:2012-12-03 08:51:23

标签: c# xml

为什么我的代码会返回错误

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

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

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

这是fix.Sorry

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);
                    }
                }
        }

0 个答案:

没有答案