1

I am trying to get the value of a div using agilitypack.my html code is like this :

<div class="div_5">
                <p>First Paragraph</p>
                <p>Second Paragraph</p>
                <p>Third Paragraph</p>
                <p>Fourth Paragraph</p>

<div class="div_6">
                <p>First Paragraph</p>
                <p>Second Paragraph</p>
                <p>Third Paragraph</p>
                <p>Fourth Paragraph</p>
     </div>
                <p>other Paragraph</p>
                <p>other Paragraph</p>
  </div>

I need the content of div_5 without the content of div_6,so i use this code :

    newsContent.Content = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']").InnerHtml;

But this code contains div_5 and div_6.how can i remove div_6 from my value ?

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180

3 Answers3

1

final code:

HtmlNode doc = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']");
                    HtmlNode node = doc.SelectSingleNode("//div[@class='div_6']");
                    node.ParentNode.RemoveChild(node);
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
0

Remove the innernode first and then proceed.

var yourNode = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']")
var toBeRemoved = resultat1.DocumentNode.SelectSingleNode ("//div[@class='_div_6']");

yourNode.RemoveChild(toBeRemoved,false);
//proceed with your code
newsContent.Content = yourNode.InnerHtml;  
Vishnu Prasad V
  • 1,228
  • 1
  • 8
  • 18
-1

I've never used AgilityHTML, but try something along these lines:

var div5 = resultat1.DocumentNode.SelectSingleNode("//div[@class='div_5']");

var div6 = div5.DocumentNode.SelectSingleNode("//div[@class='div_6']");

div6.Remove();

newsContent.Content = div5.InnerHtml;
Jens Meinecke
  • 2,904
  • 17
  • 20