How is it possible to remove all rows from the HTML table using anglesharp?

╄→尐↘猪︶ㄣ 提交于 2020-01-28 11:38:11

问题


How is it possible to remove all rows from the HTML table using anglesharp?

I am trying to remove all rows in table. I've read this documentation, however, rows are not removed.

My code looks like this:

public void DeleteRows(IElement table)
{
    foreach (var row in table?.QuerySelectorAll("tr"))
    {
        row.Remove();
    }
    var lengthAfterDeletion = table?.QuerySelectorAll("tr")?.Length;
}

How is it possible to remove all rows from the HTML table using anglesharp?


回答1:


I think we figured it out in the meantime (see https://github.com/AngleSharp/AngleSharp/issues/838).

Just for future reference (in case somebody else runs into the same problem, which happens quite easily):

public void DeleteRows(IElement table)
{
    var rows = table?.QuerySelectorAll("tr").ToArray();
    foreach (var row in rows)
    {
        row.Remove();
    }
    var legnthAfterDeletion = table?.QuerySelectorAll("tr")?.Length;
}

The key is to get a static snapshot of the result before changing the DOM. Hence the .ToArray() which comes from LINQ and can be applied to any iterator.



来源:https://stackoverflow.com/questions/59784594/how-is-it-possible-to-remove-all-rows-from-the-html-table-using-anglesharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!