2017-10-01 28 views
0

HTMLタグの前にカラーコードを追加します。C#の - このような私は(文字列形式で)HTMLに取りHTMLカラーコードリーダーを持っている

var str = @"<html><head><title> HTML highlight test page </title> </head> <body> This is text in the body.<br><h1> This is a heading </h1><p> This is a paragraph.</p> There is more text in the body after the paragraph. <p> So is this.</p> </body> </html>"; 

私は、たとえば、すべての<p>タグを利用したいと思いますそして、私はHTMLの敏捷性パックを持って

\color[DARKGRAY]<p>This is a paragraph.</p>  

\color[DARKGRAY]

それに
<p>This is a paragraph.</p> 

を追加このように

var html = doc.DocumentNode.SelectNodes("//p"); 
      if (html != null) 
      { 
       foreach (HtmlAgilityPack.HtmlNode item in html) 
       { 
        item.Name = "\color[RED]<p>"; 
       } 
      } 

しかし、それは本当に間違っています。どのように私は追加を達成することができますか?

答えて

1

すでに段落ノードを選択していますが、ループ内でInsertBeforeを使用してテキストを追加してください。

item.ParentNode.InsertBefore(doc.CreateTextNode(@"\color[RED]"), item); 
関連する問題