How to read contents of html table with .net?

J

Jim S

I have a need to read the contents of an html table on a remote web page into
a variable. I guess this is called screen scraping but not sure. I'm not
sure where to start or what the best practices are to accomplish this. For
instance; I have a healthcare app that need to check a gov't we page for a
user's license no# periodically. There is no login and I can put the user
info in the request URL no problem but not sure how to read the response data
in the tables. What is the namespace and class(s) I should be looking at?
Nothing jumped out at me under System.Web.
Thanks, Jim
 
U

UsualDosage

There are two steps to this:

First, you will need to access the HTMLDocument object for the HTML web
page. If you don't know how to do this, let me know, and I'd be glad to post
some code for it.

Next, you'll need to get the TABLE element from the HTMLDocument, loop
through the rows, and pull the data out. I've done this before using a
DataTable, which seemed to work extremely well. At any rate, here is the code
to loop through the HTML TABLE element, and get row and element information.

HTMLDocument hdoc = (HTMLDocument)html;
IHTMLElementCollection tCol = hdoc.getElementsByTagName("TABLE");
// Loop through table elements in the document
foreach (object item in tCol)
{
HTMLTableClass tbl = (HTMLTableClass)item;
// Loop through table rows
foreach (object tablerow in tbl.getElementsByTagName("TR"))
{
HTMLTableRowClass row = (HTMLTableRowClass)tablerow;
//We're going to create a string array to store the cell data.
string[] data = new string[row.getElementsByTagName("TD").length];
//Loop through the elements, and get the innertext, then add it to the
//string array
int index=0;
foreach (object element in row.getElementsByTagName("TD"))
{
HTMLTableCellClass cell = (HTMLTableCellClass)element;
data[index] = cell.innerText;
index++;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top