问题描述
如何使用anglesharp从HTML表中删除所有行?
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.
我的代码如下:
public void DeleteRows(IElement table)
{
foreach (var row in table?.QuerySelectorAll("tr"))
{
row.Remove();
}
var lengthAfterDeletion = table?.QuerySelectorAll("tr")?.Length;
}
如何使用anglesharp从HTML表中删除所有行?
How is it possible to remove all rows from the HTML table using anglesharp?
推荐答案
我认为我们在此期间已经解决了这一问题(请参见 https://github.com/AngleSharp/AngleSharp/issues/838 ).
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;
}
关键是在更改DOM之前获取结果的静态快照.因此,来自LINQ的 .ToArray()
可以应用于任何迭代器.
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.
这篇关于如何使用anglesharp从HTML表格中删除所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!