DIV后vb.net HtmlAgilityPack插入串vb、DIV、HtmlAgilityPack、net

2023-09-05 03:22:54 作者:捂住眼睛却捂不住眼泪

我想一个div结束后直接镶嵌一些我自己的HTML。这div有其他分区里面它。

I'm trying to inset some of my own html directly after the end of a div. This div has other div inside of it.

    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)

我每次运行code我得到一个异常节点&LT; D​​IV CLASS =profile_topfriends&GT;&LT; / DIV&gt;中在集合中未找到

推荐答案

类似的 的XmlNode InsertAfter() ,你需要调用参考节点的公共父这种方法是插入的节点。尝试是这样的:

Similar to XmlNode's InsertAfter(), you need to call this method on the common parent of referenced node and to be inserted node. Try something like this :

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

正常工作对我来说。下面是一个简单的测试,我在C#中做的事(翻译成VB):

Worked fine for me. Here is a simple test I did in C# (translated 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())

&LT;跨度&GT; 之后出现&LT; D​​IV&GT; 控制台