1

XML ファイルを読み込んでおり、そのデータをTreeViewC# で使用しています。私のプログラムはエラーを返します

System.Windows.Forms.dll で、タイプ 'System.StackOverflowException' の未処理の例外が発生しました。

なんで?

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>

C# コード

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++;   
    }
}
4

1 に答える 1

1

あなたのコードを実行しましたが、例外はありませんでしたが、あなたのコードにバグが見つかりました:

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

親ノードを再度追加しています。次のように変更します。

topicTree.Nodes[i].Nodes.Add(nodes);
于 2012-12-03T04:41:01.597 に答える