TreeNode 上 IEnumerator 的参数
TreeNode 上 IEnumerator 的有效参数是什么?
我在这一行收到错误:
IEnumerator ie = tn.Nodes.GetEnumerator();
在此方法中:
private void parseNode(TreeNode tn)
{
IEnumerator ie = tn.Nodes.GetEnumerator();
string parentnode = "";
parentnode = tn.Text;
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode)ie.Current;
if (ctn.GetNodeCount(true) == 0)
{
_nodeToString += ctn.Text;
}
else
{
_nodeToString += "<" + ctn.Text + ">";
}
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
_nodeToString += "</" + parentnode + ">";
_nodeToString += "\n";
}
what is the valid argument for IEnumerator on TreeNode?
i got the error on this line:
IEnumerator ie = tn.Nodes.GetEnumerator();
in this method:
private void parseNode(TreeNode tn)
{
IEnumerator ie = tn.Nodes.GetEnumerator();
string parentnode = "";
parentnode = tn.Text;
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode)ie.Current;
if (ctn.GetNodeCount(true) == 0)
{
_nodeToString += ctn.Text;
}
else
{
_nodeToString += "<" + ctn.Text + ">";
}
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
_nodeToString += "</" + parentnode + ">";
_nodeToString += "\n";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(来自评论)
这意味着您已经切换到后者
并且不会
切换到后者并且它会起作用。
IEnumerable
/IEnumerator
是非通用 API。或者更好:使用
foreach
- 它更简单、更安全、更正确(例如,您不记得检查IDisposable
)。附加说明:
StringBuilder
这样的东西是首选,远远手动XmlWriter
是理想的选择,所以 IMO:使用
XmlWriter
写入StringBuilder
,使用foreach
未经测试,但有些东西喜欢:
(from comments)
That means you have
and not
switch to the latter and it will work.
IEnumerable
/IEnumerator
are the non-generic API.Or better: use
foreach
- it is simpler, safer and more correct (you didn't remember to check forIDisposable
, for example).Additional notes:
StringBuilder
is preferred by a long marginXmlWriter
would be idealso IMO: use an
XmlWriter
that writes to aStringBuilder
, usingforeach
Untested, but something like: