Gabriel.Linden Gabriel.Linden - 2 days ago 4
Vb.net Question

Parsing htmlagilitypack (table without id's) vb.net

Hope to get some answers from you.

I use vb.net and htmlagilitypack to fetch data and it works, but not the way I want it to =)

I have this html page (part of):




<TABLE WITH=100% BORDER=4>

<TR>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=Lok" >Lok</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=Avg" >Avg&aring;r</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=AvgS" >Station</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=Ank" >Ankommer</A></TH>
<TH><A HREF="http:/cgi-bin/vplata.py?tgnr=4300&val=Visa+T%C3%A5gnummer&Bek=Visa&sort=AnkS" >Station</A></TH>
<TH>Tjänstetyp</TH>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1176&val=Visa+Lokindivid&Bek=Visa">R1176</a></TD>
<TD>Mar-20-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-20-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1267&val=Visa+Lokindivid&Bek=Visa">R1267</a></TD>
<TD>Mar-20-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-20-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1267&val=Visa+Lokindivid&Bek=Visa">R1267</a></TD>
<TD>Mar-20-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-20-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-21-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-21-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-21-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-21-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=RXXXXX&val=Visa+Lokindivid&Bek=Visa">RXXXXX</a></TD>
<TD>Mar-21-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-21-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>B1\B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-25-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-25-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1281&val=Visa+Lokindivid&Bek=Visa">R1281</a></TD>
<TD>Mar-25-2013 22:05:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>Mar-25-2013 22:28:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=KB%&val=Visa+Driftplats&Bek=Visa">KBÄ</A></TD>
<TD>D1</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=R1254&val=Visa+Lokindivid&Bek=Visa">R1254</a></TD>
<TD>Mar-27-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-27-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B2</TD>
</TR>
<TR>
<TD><a HREF="/cgi-bin/vplata.py?individ=RXXXXX&val=Visa+Lokindivid&Bek=Visa">RXXXXX</a></TD>
<TD>Mar-27-2013 13:04:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=HBGB&val=Visa+Driftplats&Bek=Visa">HBGB</A></TD>
<TD>Mar-27-2013 21:21:00</TD>
<TD><A HREF="/cgi-bin/vplata.py?stn=ET3&val=Visa+Driftplats&Bek=Visa">ET3</A></TD>
<TD>B1\B2</TD>
</TR>
</TABLE>
<A><A>Senast uppdaterad: Mar-20-2013 18:16:00</A><BR>
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="20%" bgcolor="#009900" align="left">
<IMG src="http://litmgc101.greencargo.com/bottenbild.jpg" alt="Green Cargo" width=800 height=25 border=0>
</TD>
</TR>
<TR>
</table>





What I want to do is to fetch the parts with (for example) "R1176" and the date "Mar-20-2013 13:04:00". (Would prefer to NOT have the time "13:04:00"), but I can delete that in VB.net later if I can't skip it in the parsing phase.

So to simply explain what I want to do is following:
Get all the "R1234" and the date that comes with it then put it in let's say a textbox for the "R4321" and another textbox for the date or something.

Answer

In C# I'd do something like this:

var result =
    doc.DocumentNode.SelectNodes("//td/a[contains(@href,'Lokindivid')]")
       .Select(node => new KeyValuePair<string, DateTime>(node.InnerText, DateTime.Parse(node.SelectSingleNode("./ancestor::tr[1]/td[2]").InnerText).Date));

My VB.NET foo resulted in the following code (which is a literal translation) which works with the sample html you provided:

Dim doc As New HtmlDocument
doc.LoadHtml(Content.Html)

Dim items = doc.DocumentNode.SelectNodes("//td/a[contains(@href,'Lokindivid')]").Select(Function(node) New KeyValuePair(Of String, DateTime)(node.InnerText, DateTime.Parse(node.SelectSingleNode("./ancestor::tr[1]/td[2]").InnerText).Date))

For Each item As KeyValuePair(Of String, Date) In items
    Console.WriteLine(item.Key)
    Console.WriteLine(item.Value)
Next
Comments