Solved

HTMLAgilitypack -> selectSingleNode producing the same text within a loop

Posted on 2014-01-31
4
1,650 Views
Last Modified: 2014-01-31
Hi all,

I have got the below script almost working but each time it loops through and produces the text withing td class 'home' it produces the exact same text each time....

For Each div As Object In htmlDoc.DocumentNode.SelectNodes("//div[@class='matches']")' select all the divs within the code that contain *

		lblHTMLOutput.Text +=  div.("//td[@class='home']").innerText & "<br>"
        Next

Open in new window



Here is the HTML its loooking thorough

<div id="matches">

    <div style="display:block" class="matches" id="matcheshd">
            <a id="lnkCount" href="javascript:;">2 Matches</a>
            <a id="lnkHidden" href="javascript:;">Show Hidden (<span id="hRows">0</span>)</a>
            <a id="btnAudio" href="javascript:;"></a>
            <a id="btnTextSize" href="javascript:;"></a>
    </div>
            <div class="league-header">
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <img src='/content/images/flags/46.png' alt=''/>
                            <span>International - Club Friendly</span>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="1084278" style="display:block" class="matches">
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td class="Ma_A flag">
                            <a href="javascript:;" class="x">x</a> 
                            <span id="ln-1084278" style="position:relative;">INT CF
                                <span class="lfname">Club Friendly</span>
                            </span>
                            <input type="hidden" value="1" id="sts-1084278" />
                        </td>
                        <td class="time"> 
                                    <span id="ptime-1084278" class="timezone" style="position: relative;">
                                        32
                                    </span>
                                    <span class="kickoff">08:30</span>
                                <span class="date datetimezone">01/31/2014 08:30 GMT</span>
                                <span class="gmtdatetime" style="display:none">01/31/2014 08:30</span>
                        </td>
                        <td class="home">
                             <span id="hg-1084278">
                            </span>
                            <span id="hn-1084278">SWQ Thunder</span>
                            <span class="card" id="hc-1084278">0</span>
                        </td>
                        <td class="score">
                        
                                <a href="javascript:;" class="scorelink score" matchid="1084278" leaguetitle="Club Friendly" date="1/31/2014 8:30:00 AM">
                                <span id="hs-1084278">0</span> - 
                                <span id="as-1084278">2</span><br />
                                </a>
                        </td>
                        <td class="away">
                            <span id="an-1084278">Brisbane Wolves</span>
                            <span id="ag-1084278">
                            </span>
                            <span class="card" id="ac-1084278">0</span>
                        </td>
                        <td id="live-1084278" class="liveicon"></td>
                        <td class="icons">
                            <a href="/Video/1084278" class="video" title="highlights" style="visibility:hidden"></a>
                            <a id="mm-1084278" class="gccmymatch " href="javascript:;"></a>
                            <a id="setting-1084278" class="sicon" href="javascript:;" title="Pick your own sound"></a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="league-header">
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <img src='/content/images/flags/1.png' alt=''/>
                            <span>Australia - League A</span>
                        </td>
                    </tr>
                </table>
            </div>
            <div id="1018822" style="display:block" class="matches">
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td class="Ma_A flag">
                            <a href="javascript:;" class="x">x</a> 
                            <span id="ln-1018822" style="position:relative;">AUS D1
                                <span class="lfname">League A</span>
                            </span>
                            <input type="hidden" value="1" id="sts-1018822" />
                        </td>
                        <td class="time"> 
                                    <span id="ptime-1018822" class="timezone" style="position: relative;">
                                        32
                                    </span>
                                    <span class="kickoff">08:30</span>
                                <span class="date datetimezone">01/31/2014 08:30 GMT</span>
                                <span class="gmtdatetime" style="display:none">01/31/2014 08:30</span>
                        </td>
                        <td class="home">
                             <span id="hg-1018822">
                            </span>
                            <span id="hn-1018822">Melbourne Heart FC</span>
                            <span class="card" id="hc-1018822">0</span>
                        </td>
                        <td class="score">
                        
                                <a href="javascript:;" class="scorelink score" matchid="1018822" leaguetitle="Australia League A" date="1/31/2014 8:30:00 AM">
                                <span id="hs-1018822">0</span> - 
                                <span id="as-1018822">0</span><br />
                                </a>
                        </td>
                        <td class="away">
                            <span id="an-1018822">Sydney FC</span>
                            <span id="ag-1018822">
                            </span>
                            <span class="card" id="ac-1018822">0</span>
                        </td>
                        <td id="live-1018822" class="liveicon"></td>
                        <td class="icons">
                            <a href="/Video/1018822" class="video" title="highlights" style="visibility:hidden"></a>
                            <a id="mm-1018822" class="gccmymatch " href="javascript:;"></a>
                            <a id="setting-1018822" class="sicon" href="javascript:;" title="Pick your own sound"></a>
                        </td>
                    </tr>
                </table>
            </div>

Open in new window



It loops through 3 times and each time displays

SWQ Thunder 0
SWQ Thunder 0
SWQ Thunder 0

when it should show

SWQ Thunder 0
Melbourne Heart FC 0
0
Comment
Question by:runnerjp2005
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39823459
Your code seems to be failing to compile.

In my example i have this:

For Each div as Object in htmlDoc.DocumentNode.SelectNodes("//div[@class='matches']//td[@class='home']")
	console.writeline(div.InnerText.Trim)
Next

Open in new window


This only gets the required results.

Giannis
0
 

Author Comment

by:runnerjp2005
ID: 39823473
What if i want to display several things within the div -

 <td class="time">
 <td class="home">
 <td class="away">
<td class="score">
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39823586
Why not just make your initial XPath more explicit:
For Each div As Object In htmlDoc.DocumentNode.SelectNodes("//div[@class='matches']//td[@class='home']")
    lblHTMLOutput.Text &= div.innerText & "<br>"
Next

Open in new window

0
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39823588
May i suggest that you use some LINQ:

	Dim htmlDoc = new HtmlDocument()
	htmlDoc.Load(sr)
	Dim attributes = New List(Of String)
	attributes.Add("time")
	attributes.Add("home")
	attributes.Add("away")
	attributes.Add("score")
	
	For Each div  in htmlDoc.DocumentNode.SelectNodes("//div[@class='matches']")
	   	Dim result = div.Descendants _
			.Where(Function(x) attributes.Contains(x.Attributes.Where (Function(a) a.Name = "class").Select(Function(a) a.Value).SingleOrDefault)) _
			.Select (Function(x) New With {.ClassName = x.Attributes.Where (Function(a) a.Name = "class").Select(Function(a) a.Value).SingleOrDefault,.Text = x.InnerText})
	   	For Each element as Object in result
			Console.WriteLine(element)
	   	Next
	Next

Open in new window


In the example  have a list of strings named attributes. In there a load all the classes i am interested to display.
Then using LINQ i get the results i need.


Giannis
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

18 Experts available now in Live!

Get 1:1 Help Now