C# 加载XML文件将返回System.StackOverflowException
我正在读取一个XML文件,并使用C#中的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"/>
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);