循环通过'for each as'反向循环

时间:2017-12-13 19:54:00

标签: vb.net loops

希望通过此循环向后搜索并且无法确定格式。

For Each subele As HtmlElement In WebBrowser1.Document.GetElementsByTagName("div")

提前致谢

1 个答案:

答案 0 :(得分:2)

你做不到。无法控制For Each循环方向。 你可以做的是用for循环替换它:

'' Note: Option infer should be set to ON for this line!
Dim elements = WebBrowser1.Document.GetElementsByTagName("div")
For i as Integer = elements.Length To 0 Step -1
    Dim subele As HtmlElement = elements(i)
Next

如果您只需要从GetElementsByTagName返回的最后一个元素,则可以执行此操作:

Dim elements = WebBrowser1.Document.GetElementsByTagName("div")
Dim subele As HtmlElement = elements(elements.Length-1) ' or perhaps Count, I'm not sure if this method returns an array, a list, or simply an IEnumerable...