vb.net HtmlAgilityPack Insert string after div

.net html html-agility-pack vb.net

Question

I'm attempting to insert some of my own html just after a div's conclusion. Other divs are included inside this div.

    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)

I encounter an exception each time I execute that code.Node "<div class="profile_topfriends"></div>" was not found in the collection

1
4
8/15/2014 9:43:17 PM

Accepted Answer

You must use this method on the common parent of the referred node and the node to be inserted, just as with XmlNode 's InsertAfter(). As an example, try this:

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

For me, it was perfect. Here is a simple test I wrote in C# and converted to 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())

The <span> occurs after<div> the console.

4
8/16/2014 12:29:30 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow