Select Nodes between Comments using HTMLAgilityPack

c# comments html-agility-pack

Question

I'm changing some head script for a certain widget. I need to be able to detect every node connected to that widget that is situated between the comments. Additionally, I want to quickly delete any code (including the start and end comments) that is connected to the selected widget.

The code for adding and removing items will 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-->
1
8
9/1/2011 7:15:07 PM

Accepted Answer

Attempt 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);
10
9/1/2011 8:20:23 PM

Popular Answer

I'd recommend anything along these lines:

        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);

The nodes between the two comments are eliminated (and the comments themselves).



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