2017-07-06 10 views
0
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
      string html = null; 
      html = 
      "<body> " + 
       "<p class=\"hang12\">“What is Lorem Ipsum?” <i>Lorem Ipsum is simply dummy text</i> Lorem Ipsum has been the</p>" + 
       "<p class=\"hang12\">when an unknown printer took a galley of type <i>It has survived not only five centuries,</i>.</p>" + 
       "<p class=\"hang12\">but also the <i>remaining essentially </i> </p>" + 
       "<p class=\"hang12\">with the release of Letraset sheets containing Lorem Ipsum passages, <i>and more recently with desktop</i>. 1944.</p>" + 
       "</body>"; 

      doc.LoadHtml(html); 
      foreach (var item in doc.DocumentNode.Descendants()) 
      { 
       chNodes(item); 
      } 

public void chNodes(HtmlAgilityPack.HtmlNode node) 
     { 
      try 
      { 
       if (node.HasChildNodes) 
       { 
        foreach (var item in node.ChildNodes) 
        { 
         chNodes(item); 
        } 
       } 
       else 
       { 
        Console.WriteLine("************"); 
        Console.WriteLine(node.Line); 
        Console.WriteLine(node.LinePosition); 
        Console.WriteLine("************"); 
       } 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.StackTrace); 
       throw ex; 
      } 
     } 

上記のコードは、最初に見つかった開始タグの位置を取得します。しかし、私は終了タグの位置を得ることができません。どうすれば解決できますか?これらの値は、Webブラウザコントロールのテキストを強調表示するために必要です。ありがとうございました。 ( - node.Name.Length - 3 node.LinePosition + node.OuterHtml.Length);,あなたがする必要があるかもしれませんあなたは、次のコードを使用して取得することができますhtmlagilityを使用してタグとそのテキストを取得する方法

+1

を試してみてください正確な位置に到達するために値3を調整すると、これは閉じるタグを持つすべてのノードで動作するはずだと思います – Sujith

+0

ありがとうございました。私はそのような解決策を実現しませんでした –

+0

それはうまくいきましたか? – Sujith

答えて

0

はConsole.WriteLineをのようなものをやってみこの

foreach (var item in doc.DocumentNode.SelectNodes("//p[@class='hang12']")) 
{ 
    item.innerText; 
    item.innerHtml; 
} 
関連する問題