NinjaMan
NinjaMan

Reputation: 55

HtmlAgilityPack adding nodes to other nodes

I am using HtmlAgilityPack to parse my html. My main goal is add some div elements around table row elements. However there are also some more elements in the table row. I only want to wrap it with div elements when the font's style is a background color of lightgreen. Here's an example of what my html looks like:

 <tr>
        <td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">Tove</font></td>
        <td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">To</font></td>
 </tr>

however my current code:

HtmlNode[] nodes = document.DocumentNode.SelectNodes("//tr[//td[//font[@style='background-color: lightgreen']]]").ToArray();
        foreach (HtmlNode node in nodes)
        {

               node.InnerHtml = node.InnerHtml.Replace(  node.InnerHtml,"<div class=\"select-me\">" +node.InnerHtml + "</div>");
        }

produces this html:

    <tr>
        <div class="select-me">
           <td style="padding-left: 25pt;"><font style="background-color: white" color="black">&lt;to</font><font style="background-color: white" color="black">&gt;</font></td>
           <td style="padding-left: 25pt;"><font style="background-color: white" color="black">&lt;to</font><font style="background-color: white" color="black">&gt;</font></td>
        </div>
     </tr>
     <tr>
        <div class="select-me">
           <td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">Tove</font></td>
           <td style="padding-left: 40pt;"><font style="background-color: lightgreen" color="black">To</font></td>
        </div>
     </tr>
     <tr>
        <div class="select-me">
           <td style="padding-left: 25pt;"><font style="background-color: white" color="black">&lt;/to&gt;</font></td>
           <td style="padding-left: 25pt;"><font style="background-color: white" color="black">&lt;/to&gt;</font></td>
        </div>
     </tr>

the div elements don't surround the tr elements and every tr has a div element, when only a tr element that has font element of background color lightgreen should contain a div element. If you look at the first tr element it has 4 font elements, instead of two. Ideally, my goal is to insert div elements around a tr element when its font's element background is lightgreen. I have looked at other posts, but I am still having trouble.

Correct html should like:

 <div class="select-me">
        <tr>
           <td style="padding-left: 25pt;"><font style="background-color: lightgreen" color="black">hello</font></td>
           <td style="padding-left: 25pt;"><font style="background-color: lightgreen" color="black">goodbye</font></td> 
        </tr>
     </div>

Upvotes: 1

Views: 1220

Answers (1)

user1111
user1111

Reputation: 2030

for (var i = 0; i < nodes.Length; i++ )
{
    var node = nodes[i];
    node = HtmlNode.CreateNode(node.OuterHtml.Replace(node.OuterHtml, "<div class=\"select-me\">" + node.OuterHtml + "</div>"));
}

Upvotes: 1

Related Questions