0

How do I parse HTML to find an anchor (<a />) element with specific id or class or title?

Ultimately, I want to find the value of the href attribute.

Richard
  • 56,349
  • 34
  • 180
  • 251
SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • possible duplicate of [What is the best way to parse html in C#?](http://stackoverflow.com/questions/56107/what-is-the-best-way-to-parse-html-in-c) – Alex K. Sep 06 '12 at 15:09
  • i know there is a duplicate thread(http://stackoverflow.com/questions/1428586/search-for-form-elements-in-httpwebresponse-c). but those answers were not enough for me... – SilverLight Sep 06 '12 at 15:11
  • What was the issue with the Agility Pack? – Alex K. Sep 06 '12 at 15:12
  • would you please learn me by an example(answering my Q)? there is no codes in the other thread's answers! – SilverLight Sep 06 '12 at 15:15
  • Every single line that you posted was irrelevant. Hope you don't mind my edit, which just gets to the point. – tomfanning Sep 06 '12 at 16:40

1 Answers1

1

Here is an example

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(somehtmlstring);

string value = doc.DocumentNode.Descendants("a")
    .First(n => n.Attributes["class"].Value == "someclass")
    .Attributes["href"]
    .Value;
L.B
  • 114,136
  • 19
  • 178
  • 224