Ivan-Mark Debono Ivan-Mark Debono - 2 months ago 10
C# Question

How to get list of elements by partial class name?

I have an HTML document in which there is a table with multiple rows defined as:

<tr class="row_1"></tr>
<tr class="row_2"></tr>
...
<tr class="row_10"></tr>


The total number of rows is unknown.

Is it possible to get all the elements (rows) that start with the class name
row_
?

Answer

"Is it possible to get all the elements (rows) that start with the class name row_?"

Sure, it is possible. You can either use XPath or LINQ to express your query when using HAP :

HtmlDocument doc;
....
....

var resultXPath = doc.DocumentNode
                     .SelectNodes("//tr[starts-with(@class, 'row_')]");
var resultLINQ = doc.DocumentNode
                    .Descendants("tr")
                    .Where(o => o.GetAttributeValue("class","").StartsWith("row_"));