How to select elements using HTML agility pack include selector element?

c# html-agility-pack

Question

How can I incorporate this selection in a select element by class? Example:

<div class="bla">
  <p>Some text1</p>
</div>
<div class="bla">
  <p>Some text2</p>
</div>

if use

 html.DocumentNode.SelectNodes("//div[@class='bla']")

then we only receive<p> Several Text</p> and <p> a few texts</p> I need a selector element to include in my HTML like this.

<div class="bla">

<p> a few texts</p>

</div>

help)))

1
3
7/10/2015 1:49:31 AM

Accepted Answer

You may proceed to use//div[@class='bla'] selector and get the matching HTML markupdiv s fromOuterHtml a property, as in:

var html = @"<div>
    <div class='bla'>
      <p>Some text1</p>
    </div>
    <div class='bla'>
      <p>Some text2</p>
    </div>
</div>";
var doc = new HtmlDocument();
doc.LoadHtml(html);

var nodes = doc.DocumentNode.SelectNodes("//div[@class='bla']");
foreach(HtmlNode node in nodes)
{
    Console.WriteLine(node.OuterHtml);
    Console.WriteLine();
}

Dotnetfiddle Demo

output:

<div class='bla'>
      <p>Some text1</p>
    </div>

<div class='bla'>
      <p>Some text2</p>
    </div>
1
7/10/2015 8:01:56 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow