HTMLAgilityPack Choose nodes between comments

I am replacing some script head that refers to a specific widget. I want to find all the nodes related to this widget located between comments. In addition, I want to easily remove any code associated with the specified widgets (including the start and end comments.

The insert and remote code will look like this:

<!-- WidgetScript_WidgetName --> <script src="Widgets/jquery.somecode.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $('.someid).dothis({parameter, avatar_size: 48, count: 6}); }); </script> <link href="Widgets/jquery.somecode.css" media="all" rel="stylesheet" type="text/css"/> <!--WidgetScript_WidgetName End--> 
+7
source share
2 answers

Try using the following:

 var startNode = document.DocumentNode.SelectSingleNode("//comment()[contains(., 'WidgetScript_WidgetName')]"); var endNode = document.DocumentNode.SelectSingleNode("//comment()[contains(., 'WidgetScript_WidgetName End')]"); int startNodeIndex = startNode.ParentNode.ChildNodes.IndexOf(startNode); int endNodeIndex = endNode.ParentNode.ChildNodes.IndexOf(endNode); var nodes = startNode.ParentNode.ChildNodes.Where((n, index) => index >= startNodeIndex && index <= endNodeIndex).Select(n => n); 
+9
source

I would suggest something like this:

  var head = document.DocumentNode.SelectSingleNode("html/head"); var nodes = new List<HtmlNode>(); bool isComment = false; foreach (var node in head.ChildNodes.ToList()) { if (node.NodeType == HtmlNodeType.Comment && node.InnerText.Contains("WidgetScript_WidgetName")) { isComment = !isComment; node.Remove(); } else if (isComment) { nodes.Add(node); node.Remove(); } } Console.WriteLine(head.InnerHtml); 

This removes each node between the two comments (and the comments themselves).

+3
source

All Articles