Solved

XPAth Select nodes problem

Posted on 2015-02-13
2
204 Views
Last Modified: 2015-02-15
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
Comment
Question by:PhilippeRenaud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40609235
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
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40610298
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question