Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone please explain this bit of HtmlAgilityPack code?

I've tried my best to add comments through the code but Im kind of stuck at certain parts.

// create a new instance of the HtmlDocument Class called doc
1: HtmlDocument doc = new HtmlDocument();

// the Load method is called here to load the variable result which is html 
// formatted into a string in a previous code snippet
2: doc.Load(new StringReader(result));

// a new variable called root with datatype HtmlNode is created here. 
// Im not sure what doc.DocumentNode refers to?
3: HtmlNode root = doc.DocumentNode;
4:  

// a list is getting constructed here. I haven't had much experience 
// with constructing lists yet
5: List<string> anchorTags = new List<string>();
6:  

// a foreach loop is used to loop through the html document to 
// extract html with 'a' attributes I think..      
7: foreach (HtmlNode link in root.SelectNodes("//a"))
8: {
// dont really know whats going on here
9:     string att = link.OuterHtml;
// dont really know whats going on here too
10:     anchorTags.Add(att)
11: }

I've lifted this code sample from here. Credit to Farooq Kaiser

like image 440
super9 Avatar asked Dec 28 '22 03:12

super9


2 Answers

In HTML Agility Pack terms, "//a" means "Find all tags named 'a' or 'A' anywhere in the document". See XPATH docs for a more general help on XPATH (independently from HTML agility pack). So if you document looks like this:

<div>
  <A href="xxx">anchor 1</a>
  <table ...>
    <a href="zzz">anchor 2</A>
  </table>
</div>

You will get the two anchor HTML elements. OuterHtml represents the node's HTML including the node itself, while InnerHtml represents only the node's HTML content. So, here the two OuterHtml are:

  <A href="xxx">anchor 1</a>

and

<a href="zzz">anchor 2</A>

Note I have specified 'a' or 'A' because HAP implementation takes care or HTML case insensitivity. And "//A" dos not work by default. You need to specify tags using lowercase.

like image 160
Simon Mourier Avatar answered Dec 31 '22 15:12

Simon Mourier


The key is the SelectNodes method. This part used XPath to grab a list of nodes out of the HTML that matches your query.

This is where I learned my XPath: http://www.w3schools.com/xpath/default.asp

Then it just walks through those nodes that matched and gets the OuterHTML - the full HTML including the tags, and adds them to the list of strings. A List is basically just an array, but more flexible. If you only wanted the contents, and not the enclosing tags, you'd use HtmlNode.InnerHTML, or HtmlNode.InnerText.

like image 21
LoveMeSomeCode Avatar answered Dec 31 '22 15:12

LoveMeSomeCode