HtmlAgilityPack Extension: GetElementsByClass

Posted by Blake on 6/16/2013
)

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>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <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>
    /// <returns></returns>
    /// <remarks></remarks>
    [Extension()]
    public List<HtmlNode> GetElementsByClass(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;
    }