HtmlAgilityPack getting page title and H1 tags

browser html-agility-pack tags vb.net webbrowser-control

Question

Hey all i am trying to get the page title and H1 tags from a webpage by doing the following

    doc.LoadHtml(htmlSourceCode)

    txtTitle.Text = doc.GetElementsByTagName("title").InnerText()

    txtH1.Text = doc.GetElementsByTagName("H1").InnerText()

    For Each channel In doc.DocumentNode.SelectNodes(".//meta[@name='description']")
        txtDescription.Text = channel.Attributes("content").Value
    Next

The only code above that works is the txtDescription part. Both the title and H1 do not. What type of syntax do i need to use in order to get those 2 tags?

The html code looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>
    The title text is here!
</title><link rel="icon" type="image/x-icon" href="http://www.zzzz.com/favicon.ico" />
....
<div class="main-content">
    <div class="block-info">
        <div class="container">
            <div class="article">
                <h1>
                    This is the H1 tag with the text!</h1>

<p>As the 2nd held tru

Popular Answer

You can use doc.DocumentNode.SelectSingleNode("//head/title") and doc.DocumentNode.SelectNodes("//body//h1").

Or doc.DocumentNode.Descendants("title").SingleOrDefault() and doc.DocumentNode.Descendants("h1").




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why