2011-09-01 20 views
7

特定のウィジェットに関連する頭部スクリプトを置き換えています。私はコメントの間にあるそのウィジェットに関連するすべてのノードを見つけることができるようにしたい。 。また、私はHTMLAgilityPackコメント間のノードの選択

挿入を容易開始と終了のコメントなど、指定したウィジェット(に関連する任意のコードを削除すると、コードは次のようになります削除:

<!-- 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--> 

答えて

9

次を使用してみてください:

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

完全に動作します。ありがとうございました! – steve

3

私はこのような何かを示唆している:

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

このremovを2つのコメント(およびコメントそのもの)の間にあるすべてのノード。

+0

私は実際にこの要素と@jdaviesの組み合わせを見つけて要素を削除しました。 – steve

関連する問題