Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTMLAgilityPack Select Nodes between Comments

I am replacing some head script that pertains to a specific widget. I want to be able to find all nodes relating to that widget located between the comments. Also, I want to easily remove any code related to the specified widget (including the start and end comment.

The insert and removed 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-->
like image 456
steve Avatar asked Sep 01 '11 19:09

steve


1 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);
like image 156
jdavies Avatar answered Sep 23 '22 19:09

jdavies