vb.net get all attributes value using htmlagilitypack

attributes href html-agility-pack vb.net

Question

this is the html

<div id="catlist-listview" class="cat-listview cat-listbsize">  
 <ul>
  <li><a href="http://wantedlink1" rel="bookmark" title="sometitel1" class="sonra">title1</a></li>    
  <li><a href="http://wantedlink2" rel="bookmark" title="sometitel2" class="sonra">title2</a></li>
  <li><a href="http://wantedlink3" rel="bookmark" title="sometitel3" class="sonra">title3</a></li>
  <li><a href="http://wantedlink4" rel="bookmark" title="sometitel4" class="sonra">title4</a></li>
  <li><a href="http://wantedlink5" rel="bookmark" title="sometitel5" class="sonra">title5</a></li>
  <li><a href="http://wantedlink6" rel="bookmark" title="sometitel6" class="sonra">title6</a></li>
  <li><a href="http://wantedlink7" rel="bookmark" title="sometitel7" class="sonra">title7</a></li>
  <li><a href="http://wantedlink8" rel="bookmark" title="sometitel8" class="sonra">title8</a></li>
  <li><a href="http://wantedlink9" rel="bookmark" title="sometitel9" class="sonra">title9</a></li>
  <li><a href="http://wantedlink10 " rel="bookmark" title="sometitel10" class="sonra">title10</a></li>
 </ul>
</div>

and my code is

dim htmldoc as new htmldocument
htmldoc.loadhtml(source)
for each link as htmlnode in htmldoc.document.selectnodes("//*[@id='catlist-listview']/ul")
textbox3.text = link.innerhtml
next

the output is

      <li><a href="http://wantedlink1" rel="bookmark" title="sometitel1" class="sonra">title1</a></li>    
      <li><a href="http://wantedlink2" rel="bookmark" title="sometitel2" class="sonra">title2</a></li>
      <li><a href="http://wantedlink3" rel="bookmark" title="sometitel3" class="sonra">title3</a></li>
      <li><a href="http://wantedlink4" rel="bookmark" title="sometitel4" class="sonra">title4</a></li>
      <li><a href="http://wantedlink5" rel="bookmark" title="sometitel5" class="sonra">title5</a></li>
      <li><a href="http://wantedlink6" rel="bookmark" title="sometitel6" class="sonra">title6</a></li>
      <li><a href="http://wantedlink7" rel="bookmark" title="sometitel7" class="sonra">title7</a></li>
      <li><a href="http://wantedlink8" rel="bookmark" title="sometitel8" class="sonra">title8</a></li>
      <li><a href="http://wantedlink9" rel="bookmark" title="sometitel9" class="sonra">title9</a></li>
      <li><a href="http://wantedlink10 " rel="bookmark" title="sometitel10" class="sonra">title10</a></li>

i want get all and only http://wantedlink1 to http://wantedlink10 i try attributes("href") but i get only one link i want to list all the link like this :

http://wantedlink1 
http://wantedlink2 
http://wantedlink3 
.
. 
. 
http://wantedlink10

any help ??

Accepted Answer

Basically, you can change XPath for SelectNodes() to be selecting individual <a> elements instead of <ul>. Then from this point, it will be easy to iterate through the result and get href attribute one by one. Or you achieve the same using LINQ, like the following for example :

'select <a> elements'
Dim links = htmldoc.Document.SelectNodes("//*[@id='catlist-listview']/ul/li/a")
'project to IEnumerable of href attribute value'
Dim hrefs = links.Cast(Of HtmlNode)().Select(Function(x) x.GetAttributeValue("href", ""))
'join the `hrefs`, separated by newline, into one string'
textbox3.text = String.Join(Environment.NewLine, hrefs)

dotnetfiddle demo




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