HtmlAgilityPack Extension: GetElementsByClass


Here is a brief extension method that will get all elements in an HtmlDocument that have a specified class name set. This could also be done via XPath (and possibly more efficient) but not all implementations support XPath (i.e. Windows Store apps at this point). I will run this through the C# code converter also but will not have tested that portion.

VB.Net

''' <summary>
''' Returns a List of HtmlNode objects that have a specified class name.  This is case insensitive and is looping through 
''' all Descedants instead of using XPATH which would be an alternate way to accomplish this.
''' </summary>
''' <param name="hd"></param>
''' <param name="className"></param>
<Extension()> _
Public Function GetElementsByClass(hd As HtmlDocument, className As String) As List(Of HtmlNode)
    Dim hnList As New List(Of HtmlNode)
    For Each hn As HtmlNode In hd.DocumentNode.Descendants
        If hn.Attributes("class") IsNot Nothing Then
            If hn.Attributes("class").Value IsNot Nothing Then
                If String.Compare(hn.Attributes("class").Value, className, True) = 0 Then
                    hnList.Add(hn)
                End If
            End If
        End If
    Next
    Return hnList
End Function

C#

/// <summary>
/// Returns a List of HtmlNode objects that have a specified class name.  This is case insensitive and is looping through 
/// all Descedants instead of using XPATH which would be an alternate way to accomplish this.
/// </summary>
/// <param name="hd"></param>
/// <param name="className"></param>
public static List<HtmlNode> GetElementsByClass(this HtmlDocument hd, string className)
{
    List<HtmlNode> hnList = new List<HtmlNode>();
    foreach (HtmlNode hn in hd.DocumentNode.Descendants) {
        if (hn.Attributes("class") != null) {
            if (hn.Attributes("class").Value != null) {
                if (string.Compare(hn.Attributes("class").Value, className, true) == 0) {
                    hnList.Add(hn);
                }
            }
        }
    }
    return hnList;
}

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.