Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

XPAth Select nodes problem

Hi EE,

I have in vb.net a form that im using HtmlAgilityPack

im doing this :
        Dim client As WebClient = New WebClient()
        Dim reply As String = client.DownloadString("i put my URL here")
        Dim aHTML As New HtmlDocument
       
       aHTML.Load(reply)


The html is pretty long. im trying to get ALL Tags "td" that as a specific attribute
in other words, all those:  <td class="stats_1" .....

all td nodes that has a class vlaue of "stats_1"

then I would to a For loop in all results to finish what I want...

can you help me ?
Thanks
0
PhilippeRenaud
Asked:
PhilippeRenaud
1 Solution
 
Fernando SotoCommented:
Hi PhilippeRenaud;

Here is how you can do it using Linq to XML.

'' Load the XML document from the file system
Dim xdoc As XDocument = XDocument.Load("Pant and File place HERE")

'' This Linq to XML query will return all the td nodes that has 
'' a class attribute whos vlaue is "stats_1" as List(Of XElement)
Dim selectedTds = (From td in xdoc.Root.Descendants("td") _
                   Where td.Attribute("class").Value = "stats_1" _
                   Select td).ToList()

'' Iterate through all td nodes and modify them as needed
'' This For Each loop show how to access the values                   
For Each node As XElement In selectedTds
    Console.WriteLine("{0}  :  {1}  :  {2}", node.Name, node.Attribute("class").Value, node.Value)
Next

Open in new window

0
 
Miguel OzSoftware EngineerCommented:
For HtmlAgilityPack, you must use HtmlWeb to fetch the page contents using URL as shown In code below:
C# sample
var web = new HtmlWeb();
var document = web.Load(url);
var tdNodes = document.DocumentNode.SelectNodes("//td[@class='stats_1']");
//Loop tdNodes and use InnerText property to extract the node contents for attributes use Attributes["name of attribute"] property.

Open in new window

VB.NET code:
Dim web As New HtmlWeb = New HtmlWeb()
Dim doc As New HtmlDocument
doc = web.Load("Your URL here")
For Each node As HtmlNode In doc.DocumentNode.SelectNodes("//td[@class='stats_1']")
 Console.Write(node.InnerText)
Next

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now