VB.net loop through standandard html table and read cells in body of email

Hi

Is it possible to loop through the cells of an html table received in the body of an email
and read the content?

The blank table originally sent would be in the form



Accept Multiple Solutions

Accept as Solution


 



 

MlandaT


Expert Comment
 on 2015-09-23 at 07:54:22ID: 40990434





<table>
  <tr>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
You can use the HtmlAgilityPack for this sort of thing.
Dim url As String = "C:\Temp\tbl.html"
Dim FirstRowHasHeadings As Boolean = True

Dim htmlSnippet As New HtmlDocument()
htmlSnippet.Load(url)

Dim columns As Integer = 0
Dim dt As New DataTable()

Dim th = htmlSnippet.DocumentNode.SelectNodes("//table//th")
If th IsNot Nothing Then

	columns = th.Count
	For Each h As var In htmlSnippet.DocumentNode.SelectNodes("//table//th")
		dt.Columns.Add(New DataColumn(h.InnerText, GetType(String)))

	Next
Else
	Dim tr = htmlSnippet.DocumentNode.SelectNodes("//table//tr")
	If tr IsNot Nothing Then
		columns = tr(0).SelectNodes("td").Count
		If FirstRowHasHeadings Then
			For Each h As var In tr(0).SelectNodes("td")
				dt.Columns.Add(New DataColumn(h.InnerText, GetType(String)))
			Next
		End If
			'columns
	Else
	End If
End If

If dt.Columns.Count = 0 Then
	For i As Integer = 0 To columns - 1
		dt.Columns.Add(New DataColumn(String.Format("Column{0}", i), GetType(String)))
	Next
End If

For Each row As var In htmlSnippet.DocumentNode.SelectNodes("//table//tr")
	Dim dr As DataRow = dt.Rows.Add()
	If row IsNot Nothing AndAlso row.SelectNodes("./td") IsNot Nothing Then

		Dim index As Integer = 0
		For Each cell As var In row.SelectNodes("./td")
			dr(index) = cell.InnerText
                       index +=1
		Next
	End If
Next

Open in new window

<table class="w3-table-all" style="width:100%">
<tbody><tr>
	<th>Number</th>
	<th>First Name</th>
	<th>Last Name</th>		
	<th>Points</th>
</tr>
<tr>
	<td>1</td>
	<td>Eve</td>
	<td>Jackson</td>		
	<td>94</td>
</tr>
<tr>
	<td>2</td>
	<td>John</td>
	<td>Doe</td>		
	<td>80</td>
</tr>
<tr>
	<td>3</td>
	<td>Adam</td>
	<td>Johnson</td>		
	<td>67</td>
</tr>
<tr>
	<td>4</td>
	<td>Jill</td>
	<td>Smith</td>		
	<td>50</td>
</tr>
</tbody></table>

Open in new window

Also http://blog.hypercomplex.co.uk/index.php/2010/05/parsing-html-tables-into-system-data-datatable/

A tricky thing to keep in mind though is that HTML is not always perfect. The parsing presented here will work on tables that fit the expected structure. If an email client changes THs to TDs then the method might fail completely

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi murbro;

This sample code will do what you want.
Dim frag = "The HTML table data as a string Here"
'' Parse the HTML into a XML document
Dim xdocFrag As XElement = XElement.Parse(frag)
'' Create a List of XElement having the tag "tr"
Dim rows = (From row In xdocFrag.Descendants("tr")
            Select row).ToList()
'' Iterate through each row of the HTML table
For Each tr As XElement In rows
    '' Seperate lines in the output
    Console.WriteLine()
    '' Iterate through each row cell and display its value
    For Each th As XElement In tr.Elements()
        Console.Write("{0},  ", th.Value)
    Next
Next

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thank you both very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.