2010-09-29 5 views
3

Je veux supprimer des choses html commeComment sélectionner les types de nœuds qui sont HtmlNodeType.Comment en utilisant HTMLAgilityPack

<!--[if gte mso 9]> 
... 
<![endif]--> 


<!--[if gte mso 10]> 
... 
<![endif]--> 

Comment faire cela en C# en utilisant HTMLAgilityPack?

J'utilise

static void RemoveTag(HtmlNode node, string tag) 
     { 
      var nodeCollection = node.SelectNodes("//"+ tag); 
      if(nodeCollection!=null) 
       foreach (HtmlNode nodeTag in nodeCollection) 
       { 
        nodeTag.Remove(); 
       } 
     } 

pour les balises normales.

+0

Je ne suis même pas sûr que l'extrait, qui est conditionals provenant de MicrosoftWord sont HtmlNodeType.Comment – bcm

Répondre

8
 public static void RemoveComments(HtmlNode node) 
     { 
      foreach (var n in node.ChildNodes.ToArray()) 
       RemoveComments(n); 
      if (node.NodeType == HtmlNodeType.Comment) 
       node.Remove(); 
     } 


     static void Main(string[] args) 
     { 
      var doc = new HtmlDocument(); 
      string html = @"<!--[if gte mso 9]> 
... 
<![endif]--> 

<body> 
    <span> 
     <!-- comment --> 
    </span> 
    <!-- another comment --> 
</body> 

<!--[if gte mso 10]> 
... 
<![endif]-->"; 
      doc.LoadHtml(html); 

      RemoveComments(doc.DocumentNode); 
      Console.WriteLine(doc.DocumentNode.OuterHtml); 
      Console.ReadLine(); 

     } 

Ou un peu de style LINQ amusant:

public static IEnumerable<HtmlNode> Walk(HtmlNode node) 
{ 
    yield return node; 
    foreach (var child in node.ChildNodes) 
     foreach (var x in Walk(child)) 
      yield return x; 
} 

... 

foreach (var n in Walk(doc.DocumentNode).OfType<HtmlCommentNode>().ToArray()) 
    n.Remove(); 

Encore plus facile (Oublier que nous pourrions utiliser XPath pour trouver des nœuds de commentaire)

var doc = new HtmlDocument(); 
    string html = @" 
<!--[if gte mso 9]> 
... 
<![endif]--> 

<body> 
<span> 
<!-- comment --> 
</span> 
<!-- another comment --> 
</body> 

<!--[if gte mso 10]> 
... 
<![endif]-->"; 
    doc.LoadHtml(html); 
    foreach (var n in doc.DocumentNode.SelectNodes("//comment()") ?? new HtmlNodeCollection(doc.DocumentNode)) 
     n.Remove(); 
+1

+1 pour l'amour de trouver une meilleure méthode de codage pour accomplir la même tâche – bcm

0

@ Mark , a incorporé votre 3ème exemple pour produire ceci, pour référence:

public static string CleanUpRteOutput(this string s) 
     { 
      if (s != null) 
      { 
       HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
       doc.LoadHtml(s); 
       RemoveTag(doc, "script"); 
       RemoveTag(doc, "link"); 
       RemoveTag(doc, "style"); 
       RemoveTag(doc, "meta"); 
       RemoveTag(doc, "comment"); 
... 

et la fonction removeTag:

static void RemoveTag(HtmlAgilityPack.HtmlDocument doc, string tag) 
     { 
      foreach (var n in doc.DocumentNode.SelectNodes("//" + tag) ?? new HtmlAgilityPack.HtmlNodeCollection(doc.DocumentNode)) 
       n.Remove(); 
     }