Solved

XPAth Select nodes problem

Posted on 2015-02-13
2
164 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
2 Comments
 
LVL 62

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 35

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now