Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载XML文件将返回System.StackOverflowException_C#_Xml - Fatal编程技术网

C# 加载XML文件将返回System.StackOverflowException

C# 加载XML文件将返回System.StackOverflowException,c#,xml,C#,Xml,我正在读取一个XML文件,并使用C#中的TreeView数据。我的程序返回一个错误 “System.StackOverflowException”类型的未处理异常 发生在System.Windows.Forms.dll中 为什么? XML文件 <ListOfTopics> <MainTopic url="index.html" title="Homes"> <SubTopic url="index.html" title="Sub Topic1"/>

我正在读取一个XML文件,并使用C#中的
TreeView
数据。我的程序返回一个错误

“System.StackOverflowException”类型的未处理异常 发生在System.Windows.Forms.dll中

为什么?

XML文件

<ListOfTopics>
  <MainTopic url="index.html" title="Homes">
    <SubTopic url="index.html" title="Sub Topic1"/>
    <SubTopic url="index.html" title="Sub Topic2"/>
    <SubTopic url="index.html" title="Sub Topic3"/>
  </MainTopic>
</ListOfTopics>
public void LoadTopics()    
{
    XmlDocument xml = new XmlDocument();
    xml.Load("topics.xml");
    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");
        topicTree.Nodes.Add(node);

        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");
                topicTree.Nodes[i].Nodes.Add(node);
            }

        }
        i++;   
    }
}

我运行了你的代码,没有出现异常,但是我确实在你的代码中发现了一个bug:

topicTree.Nodes[i].Nodes.Add(node);
如果要重新添加父节点,请将其更改为:

topicTree.Nodes[i].Nodes.Add(nodes);

我运行了你的代码,没有出现异常,但是我确实在你的代码中发现了一个bug:

topicTree.Nodes[i].Nodes.Add(node);
如果要重新添加父节点,请将其更改为:

topicTree.Nodes[i].Nodes.Add(nodes);