Vb.net HtmlAgilityPack Insert row after div

I am trying to insert some of my own html immediately after the div ends. This div has another div inside.

Dim HtmlNode As HtmlNode = HtmlNode.CreateNode("<span class=""label"">Those were the friends</span>") Dim FriendDiv = htmldoc.DocumentNode.SelectSingleNode("//div[@class='profile_friends']") Dim NewHTML As HtmlNode = htmldoc.DocumentNode.InsertAfter(HtmlNode, FriendDiv) 

Every time I run this code, I get a Node "<div class="profile_topfriends"></div>" was not found in the collection exception Node "<div class="profile_topfriends"></div>" was not found in the collection

+5
source share
1 answer

Like XmlNode InsertAfter() , you need to call this method on the generic parent of the node link and insert the node. Try something like this:

 Dim NewHTML As HtmlNode = FriendDiv.ParentNode.InsertAfter(HtmlNode, FriendDiv) 

Worked well for me. Here is a simple test I did in C # (translated into VB):

 Dim html = "<body><div></div></body>" Dim doc As New HtmlDocument() doc.LoadHtml(html) Dim div = doc.DocumentNode.SelectSingleNode("//div") Dim span = HtmlNode.CreateNode("<span class=""label"">Those were the friends</span>") Dim newHtml = div.ParentNode.InsertAfter(span, div) Console.WriteLine(XDocument.Parse(doc.DocumentNode.OuterHtml).ToString()) 

<span> appears after the <div> in the console.

+4
source

Source: https://habr.com/ru/post/1200452/


All Articles