2011-09-21 53 views
0

似乎没有关于codeplex页面的文档,并且出于某种原因,intellisense并未向我显示可用的方法或任何htmlagilitypack (例如,当我输入MyHtmlDocument.DocumentNode。 - 没有intellisense告诉我接下来可以做什么)如何从HTML中使用HTML敏捷包(ASP.NET)删除特定元素(vb)

我需要知道如何从HTML文档的主体中删除所有< a>标签及其内容我不能仅仅在Body上使用Node.InnerText,因为它仍然返回来自A标签的内容。

这里是例如HTML

<html> 
    <body> 
     I was born in <a name=BC>Toronto</a> and now I live in barrie 
    </body> 
</html> 

我需要返回

I was born in and now I live in barrie 

谢谢,我感谢帮助!

托马斯

回答

0

这得到了你所需要的结果。这使用递归方法深入了解所有的html节点,并且可以通过添加新的if语句简单地删除更多节点。

Public Sub Test() 
    Dim document = New HtmlDocument() With { _ 
     Key .OptionOutputAsXml = True _ 
    } 
    document.LoadHtml("<html><body>I was born in <a name=BC>Toronto</a> and now I live in barrie</body></html>") 

    For i As var = 0 To document.DocumentNode.ChildNodes.Count - 1 
     RecursiveMethod(document.DocumentNode.ChildNodes(i)) 
    Next 

    Console.Out.WriteLine(document.DocumentNode.InnerHtml.Replace(" ", " ")) 
End Sub 

Public Sub RecursiveMethod(child As HtmlNode) 
    For x As var = 0 To child.ChildNodes.Count - 1 
     Dim node = child.ChildNodes(x) 
     If node.Name = "a" Then 
      node.RemoveAll() //removes all the child nodes of "a" 
      node.Remove() //removes the actual "a" node 
     Else 
      If node.HasChildNodes Then 
       RecursiveMethod(node) 
      End If 
     End If 
    Next 
End Sub 
+0

谢谢我会尝试这种方法。截至目前,我只是输出所有的HTML和设置显示:无; CSS上的a标签,直到我的老板确定我们是否需要这个'untagged'的内容。 – tsdexter

+0

没问题,很高兴我可以帮忙= o) – Bobby

1

东西线沿线的(对不起我的代码是C#,但我希望这将有助于仍然)

HtmlDocument doc = new HtmlDocument(); 

doc.LoadHtml("some html markup here"); 

HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//a[@name]"); 

foreach(HtmlNode link in links) 
{ 
    link.Remove(); 
} 

//then one of the many doc.Save(...) overrides to actually get the result of the operation.