For example, I want to extract the first definition from http://www.urbandictionary.com/define.php?term=potato . It's raw text, though.
var html = new HtmlDocument();
html.LoadHtml(new WebClient().DownloadString("http://www.urbandictionary.com/define.php?term=potato"));
var root = html.DocumentNode;
var p = root.Descendants()
.Where(n => n.GetAttributeValue("class", "").Equals("meaning"))
.Single()
.Descendants("")
.Single();
var content = p.InnerText;
This is the code I use to try and extract the meaning class. This doesn't work at all, though... How do I extract the class from Urban Dictionary?
Talk1:
You should debug your query and figure out what is wrong - one level of LINQ query at a time. If you can't figure out particular cases - make sure to provide minimal reproducible example (including minimal data inline in the post).
Solutions1
If you change your code as below it works
var html = new HtmlDocument();
html.LoadHtml(new WebClient().DownloadString("http://www.urbandictionary.com/define.php?term=potato"));
var root = html.DocumentNode;
var p = root.SelectNodes("//div[@class='meaning']").First();
var content = p.InnerText;
The text I'm using in SelectNodes is XPath and means all div elements with class named meaning. You need to use First or FirstOrDefault as the page contains multiple div elements with that class name, so Single would throw an exception.
Solutions2
Alternatively you can use, if you wanted to use the same "style" as the link you were using.
var p = root.Descendants()
.Where(n => n.GetAttributeValue("class", "").Equals("meaning"))
.FirstOrDefault();
Though Tone's answer is more elegant, one liners are usually better.