麻烦与foreach柜台 [英] Troubles with foreach counter

查看:75
本文介绍了麻烦与foreach柜台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach (HtmlAgilityPack.HtmlNode table in doc.DocumentNode.SelectNodes("//div[@class='top_b']"))
            {
            Outer:
                foreach (HtmlNode b in table.SelectNodes("b"))
                {
                    MessageBox.Show(b.InnerText.ToString());

                    foreach (HtmlNode p in table.SelectNodes("p"))
                    {
                        MessageBox.Show(p.InnerText.ToString());
                    
                        foreach (HtmlAgilityPack.HtmlNode class_table in table.SelectNodes("//table[@class='t']"))
                        {
                         
                             MessageBox.Show(class_table.InnerText.ToString());
                        
                           goto Outer;
                        }
                    }
                }
            }


当涉及到转到外部时,它将重置foreach.即,不保存计数器值.

ps/对不起,我的英语


When it comes to goto outer then it resets the foreach. That is, the counter value is not saved.

ps/ Excuse for my english

推荐答案

没有计数器.

您到底为什么要嵌套这些循环?
它们都在相同的表数据上运行,因此嵌套它们只是意味着相同的信息会被多次显示.

您是新手,所以:

从您的代码中删除转到

现在,忘记goto 至少存在三年.
然后,您将有足够的经验来制定何时应该实际使用它.这是非常非常少见的.到目前为止,我从未在任何C#代码中使用goto!

现在,找出您实际在纸上要做的事情,并将其实现.
There is no counter.

Why the heck are you nesting those loops anyway?
They all run on the same table data, so nesting them just means that the same information is shown lots of times.

You are a beginner, so:

REMOVE THE GOTO FROM YOUR CODE

Now, forget that goto exists at all for at least three years.
Then you will have enough experience to work out when you should actually use it. Which is very, very infrequently. So far, I have never used a goto in any of my C# code!

Now work out what you are actually trying to do on paper, and implement that.


这篇关于麻烦与foreach柜台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆