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
Solved

XPAth Select nodes problem

Posted on 2015-02-13
2
183 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 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

829 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