Reputation: 123
let say i have follow html document
<div class=" wrap_body text_align_left" style="">
<div class="some"> hello </div>
<div class="someother"> world </div>
hello world
</div>
i want to extract this
<div class="some"> hello </div>
<div class="someother"> world </div>
hello world
what is best way to extract using HtmlAgilityPack with c# or vb.net? this is my code until done but some struggle . thanks!
For Each no As HtmlAgilityPack.HtmlNode In docs.DocumentNode.SelectNodes("//div[contains(@class,'wrap_body')]")
Dim attr As String = no.GetAttributeValue("wrap_body", "")
Next
Upvotes: 0
Views: 2369
Reputation: 9771
You can use SelectNodes
of DocumentNode
metod to retrieve specific nodes from html.
class Program
{
static void Main(string[] args)
{
string htmlContent = File.ReadAllText(@"Your path to html file"); ;
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
var innerContent = doc.DocumentNode.SelectNodes("/div").FirstOrDefault().InnerHtml;
Console.WriteLine(innerContent);
}
}
Output:
Upvotes: 1
Reputation: 134
Below is a sample for getting Inner Html
var html =
@"<body>
<div class='wrap_body text_align_left' style=''>
<div class='some'> hello </div>
<div class='someother'> world </div>
hello world
</div>
</body>";
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//body/div");
foreach (var node in htmlNodes)
{
Console.WriteLine(node.InnerHtml);
}
Upvotes: 2