How to get title tag using HTML Agility Pack

c# html html-agility-pack

Question

I'm parsing an HTML file using HTML Agility Pack. I want to get

<title>Some title <title>

As you see, title doesn't have a class. So I couldn't catch it no matter what I have tried. I couldn't find the solution on the web either. How can I catch this HTML tag which doesn't have a class? Thanks.

Accepted Answer

This might do the trick for you

doc.DocumentNode.SelectSingleNode("//head/title");

or

doc.DocumentNode.SelectSingleNode("//title");

or

doc.DocumentNode.Descendants("title").FirstOrDefault()

Popular Answer

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);

var result = doc.DocumentNode.SelectNodes("title").FirstOrDefault();


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