Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MSHTML.Document

194 views
Skip to first unread message

Mike Tan

unread,
Jul 2, 2002, 2:17:56 PM7/2/02
to
I am using MSHTML.Ducument to pick out some data from a web page in a
windows form application. I have some problem when using the Document
object. I use the "Quick watch" to see what's in an object.

Here is my testing code:

//First I want to find out a table in a html page:
MSHTML.HTMLDocumentClass document =
(MSHTML.HTMLDocumentClass)webBrowser.Document;
MSHTML.HTMLTable tables =
(MSHTML.HTMLTable)document.getElementsByTagName("TABLE");
//This one doesn't work. nothing in "tables", in "Quick watch", it reads
it's a System._ComObject
MSHTML.IHTMLElementCollection tables =
document.getElementsByName("aax");
//This one doesn't work as well, still nothing in it
MSHTML.HTMLTable b = (MSHTML.HTMLTable)document.getElementById("xxxx");
//This line is good, a HTMLTable is returned.

//Then I want to go through all the rows in the table:
foreach(MSHTML.HTMLTableRow row in tables.rows)
{
foreach(MSHTML.HTMLTableCell cell in row)
{
txt2.Text = txt2.Text + cell.toString() + " + ";
}
txt2.Text = txt2.Text + @"\n";
}

Please tell me what's the proper way to do this. Thank you.

Mike Tan


Nicholas Paldino [.NET/C# MVP]

unread,
Jul 2, 2002, 2:43:40 PM7/2/02
to
Mike,

Here is the code you should be using:

//First I want to find out a table in a html page:
MSHTML.HTMLDocumentClass document =
(MSHTML.HTMLDocumentClass)webBrowser.Document;

//This one doesn't work. nothing in "tables", in "Quick watch", it reads
it's a System._ComObject
// MSHTML.HTMLTable tables = (MSHTML.HTMLTable)
document.getElementsByTagName("TABLE");
// This should be an instance of IHTMLElementCollection.
MSHTML.IHTMLElementCollection = (MSHTML.IHTMLElementCollection)
document.getElementsByTagName("TABLE");

//This one doesn't work as well, still nothing in it
// MSHTML.IHTMLElementCollection tables = document.getElementsByName("aax");

// Are you sure that there are elements with the id "aax" in your document?

//This line is good, a HTMLTable is returned.
MSHTML.HTMLTable b = (MSHTML.HTMLTable)document.getElementById("xxxx");

//Then I want to go through all the rows in the table:

// Here you might want to use an indexor instead.
for (int i = 0; i < tables.rows.length; ++i)
{
// Set the row.
MSHTML.HTMLTableRow row = (MSHTML.HTMLTableRow) tables.rows.item(i);

And so on.

Hope this helps.


--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com


"Mike Tan" <mike...@hotmail.com> wrote in message
news:ebtAQSfICHA.1600@tkmsftngp12...

Mike Tan

unread,
Jul 2, 2002, 3:05:56 PM7/2/02
to
Thank you, Nicholas.

But seems the problem still there.

MSHTML.IHTMLElementCollection myTables = (MSHTML.IHTMLElementCollection)
document.getElementsByTagName("TABLE");

In this line, I got a IHTMLElementCollection, Then how can I get the tables
in this collection?

Mike

Nicholas Paldino [.NET/C# MVP]

unread,
Jul 2, 2002, 3:18:06 PM7/2/02
to
Mike,

You will have to get it using the item method, which will give you a
table based on the index you pass into it. Basically, that collection is a
collection of all the tables in the document, so passing 0 will give you the
first one, 1 will give you the second one, etc, etc.


--
- Nicholas Paldino [.NET MVP]
- nicholas...@exisconsulting.com

"Mike Tan" <mike...@hotmail.com> wrote in message

news:ODkoEtfICHA.1576@tkmsftngp12...

Mike Tan

unread,
Jul 2, 2002, 11:10:58 PM7/2/02
to
Hi Nicholas,

the item method is: object item(object name, object index)
I was confused with the parameter object name, so I didn't try this method.
I use null for object name, it's ok now, just don't understand what the
"object name" means.

Thank you very much

Mike Tan

"Nicholas Paldino [.NET/C# MVP]" <nicholas...@exisconsulting.com> wrote
in message news:OtuSX2fICHA.1600@tkmsftngp12...

0 new messages