This project has moved and is read-only. For the latest updates, please go here.

Html Agility Pack - Help Skipping Table

Topics: Developer Forum, User Forum
Mar 6, 2015 at 7:12 PM
I have a small function that removes extra spaces, tabs, new lines, line breaks from a piece of html.

One of the pages that gets processed by this code, contains an HTML table, that I need to skip one of its rows from being minifed, <tr class="navtable">

The table structure is:
<table id="TopNav" class="resultsNav" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;">
    <tr class="navtable1">
            <div class="navPagesTop">
                <div class="navSelFL navPriceRefinements">
                    <select autocomplete="off" class="navSelPerPage navSelPrice" id="refShopByPrice" onchange="waitRefine(, this.value)" >
                        <option value="55_100">$55 to $100 (19)</option>
                        <option value="100_110">$100 to $110 (7)</option>
                        <option value="110_165">$110 to $165 (54)</option>
                        <option value="165_200">$165 to $200 (14)</option>
                        <option value="200_250">$200 to $250 (3)</option>
                        <option value="250_400">$250 to $400 (2)</option>
                        <option selected value="">Filter By Price</option>
                <div class="navSelFL navOrRefinements"> or </div>
                <div class="navSelFL navBrandRefinements">
                    <select autocomplete="off" class="navSelPerPage navSelBrand" id="refShopByBrand" onchange="waitRefine(, this.value)" >
                        <option value="carhartt">Carhartt&nbsp;(21)</option>
                        <option value="caterpillar">Caterpillar&nbsp;(3)</option>
                        <option value="danner">Danner&nbsp;(5)</option>
                        <option value="dingo">Dingo&nbsp;(1)</option>
                        <option value="georgia-boot">Georgia Boot&nbsp;(10)</option>
                        <option value="hi-tec">Hi-Tec&nbsp;(2)</option>
                        <option value="irish-setter">Irish Setter&nbsp;(8)</option>
                        <option value="john-deere-branded-items">John Deere Branded Items&nbsp;(8)</option>
                        <option value="keen">Keen&nbsp;(5)</option>
                        <option value="lacrosse">LaCrosse&nbsp;(1)</option>
                        <option value="rocky">Rocky&nbsp;(7)</option>
                        <option value="thorogood-shoes">Thorogood Shoes&nbsp;(4)</option>
                        <option value="timberland-pro">Timberland Pro&nbsp;(9)</option>
                        <option value="wolverine">Wolverine&nbsp;(8)</option>
                        <option selected value="">Filter By Brand</option>
    <tr class="navtable">
        <td class="navSelPerPage" align="center" colspan="5" style="white-space:nowrap;">
            <div class="navPages">
                <div class="navSelFL navArrowPrev">
                    <a class="navArrowsSelected" rel="prev" href="/plain-toe-leather?perPage=20#prdRslts"><< Previous</a>
                <div class="navSelFL navPageNumbers">
                    <span class="navSelUnSelected"><a href="/plain-toe-leather?perPage=20#prdRslts" rel="prev">&nbsp;1&nbsp;</a></span>
                    <span class="navSelSelected">&nbsp;2&nbsp;</span>
                    <span class="navSelUnSelected"><a href="/plain-toe-leather/3?perPage=20#prdRslts" rel="next">&nbsp;3&nbsp;</a></span>
                    <span class="navSelUnSelected"><a href="/plain-toe-leather/4?perPage=20#prdRslts">&nbsp;4&nbsp;</a></span>
                    <span class="navSelUnSelected"><a href="/plain-toe-leather/5?perPage=20#prdRslts">&nbsp;5&nbsp;</a></span>
                <div class="navSelFL navArrowNext">
                    <a class="navArrowsSelected" rel="next" href="/plain-toe-leather/3?perPage=20#prdRslts">Next >></a>
                <div class="navSelFL navPageList">
                    <span class="navSelBlack">Page 2 of 5</span>
And the code that does the removal of extra whie space, tabs, line breaks.
    Public Shared Function MinifyStringContent(ByVal strContent As String) As String
        Dim htmlDoc As New HtmlAgilityPack.HtmlDocument
        Dim strNoSpaces As String = Regex.Replace(strContent, "\s{2,}", " ")
        Dim oldFlags As New HtmlAgilityPack.HtmlElementFlag

        htmlDoc.OptionDefaultStreamEncoding = Encoding.UTF8

        If HtmlAgilityPack.HtmlNode.ElementsFlags.ContainsKey("option") Then
            HtmlAgilityPack.HtmlNode.ElementsFlags("option") = HtmlAgilityPack.HtmlElementFlag.Closed
            HtmlAgilityPack.HtmlNode.ElementsFlags.Add("option", HtmlAgilityPack.HtmlElementFlag.Closed)
        End If


        If Not IsNothing(strNoSpaces) AndAlso strNoSpaces <> "" Then
            For Each textnode In htmlDoc.DocumentNode.SelectNodes("//text()")
                If Not (textnode.ParentNode.Name = "script" OrElse textnode.ParentNode.Name = "style") Then
                    textnode.InnerHtml = textnode.InnerText.Replace(vbCrLf, "").Replace("  ", " ").Replace(vbTab, "").Replace(vbNewLine, "")
                End If
        End If

        Return htmlDoc.DocumentNode.OuterHtml

    End Function
Is there a htmal agility pack method that I can use on my function to tell it to not process the "TopNav" table row with class "navtable"?

If someone could show me how to achieve this that would be awesome. I am new at coding, but I will try to follow up the best I can.

Thank you very much.
Mar 6, 2015 at 8:51 PM
I think that an easy way I can do this is by finding out if the table exists in the htmlodoc

how can this be done?