Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XPAth Select nodes problem

Posted on 2015-02-13
2
Medium Priority
?
219 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 64

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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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