Valhalla Legends Forums Archive | .NET Platform | GetType() problems

AuthorMessageTime
laurion
Hi all.

I'm trying to expand my VB6 code to C#, but one of my projects is presenting a large problem.

Using Microsoft HTML DOM in VB6, I can use TypeName() to find out what kind of HTML element I'm looking at, like so:
[code]
For x = 0 To oIE.Document.All.length - 1
       
        If TypeName(oIE.Document.All(x)) = "HTMLFrameElement" Then
[/code]

Trying to do the same in C# gives me poor results. Using this code:
[code]
            HTMLDocument hdoc = new HTMLDocumentClass();
            hdoc = (HTMLDocument)this.wbMain.Document.DomDocument;

            for (int i = 0; i < hdoc.all.length ; i++)
            {
                this.txtMain.Text += hdoc.all.item(null, i).GetType().ToString() +"\r\n";
            }
[/code]
The output looks like this:

mshtml.HTMLHtmlElementClass
mshtml.HTMLHtmlElementClass
mshtml.HTMLHtmlElementClass
mshtml.HTMLHtmlElementClass
mshtml.HTMLHtmlElementClass
mshtml.HTMLHtmlElementClass
mshtml.HTMLHtmlElementClass
mshtml.HTMLHtmlElementClass

But in VB6, I'd get a more specific list, looking like

HTMLHtmlElement
HTMLHeadElement
HTMLTitleElement
HTMLScriptElement
HTMLMetaElement
HTMLMetaElement
HTMLMetaElement
HTMLStyleElement
HTMLStyleElement
HTMLScriptElement
HTMLFrameSetSite
HTMLFrameElement
HTMLFrameElement
HTMLFrameElement

Which would allow me to actually manage the objects and use them as I see fit.

Why am I just receiving HTMLElements in C#? Yes, they are all BASE HTMLElements, but it's not specific enough and it doesn't tell me anything about the object.

Basically, instead of seeing HTMLElement, I need to see HTMLFrameElement, or HTMLInputElement, etc.

Thanks in advance, all help is appreciated.
June 5, 2008, 2:22 AM
Myndfyr
I tossed this function into my JinxBot code:

[code]
        [System.Diagnostics.Conditional("DEBUG")]
        private void PrintDom(IHTMLDocument2 doc)
        {
            System.Threading.ParameterizedThreadStart ts = delegate(object param)
            {
                IHTMLDocument2 doc2 = param as IHTMLDocument2;
                StringBuilder sb = new StringBuilder();
                foreach (IHTMLElement el in doc2.all)
                {
                    sb.AppendLine(el.GetType().ToString());
                }
                textBox1.Text = sb.ToString();
            };
            if (InvokeRequired)
                Invoke(ts, doc as object);
            else
                ts(doc);
        }
[/code]

Here's the input:
[code]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JinxBot Display Interface</title>
<style type="text/css">
body
{
    background-color: black;
    font-size: 12px;
    color: #dddddd;
    font-family: Tahoma, Verdana, Sans-serif;

}

p
{
    text-indent: -3em;
    margin-left: 3em;
    margin-top: 4px;
    margin-bottom: 0px;
}
</style>
</head>
<body>
    <div id="enterText"></div>
</body>
</html>
[/code]

Here's the result:
mshtml.HTMLCommentElementClass
mshtml.HTMLHtmlElementClass
mshtml.HTMLHeadElementClass
mshtml.HTMLTitleElementClass
mshtml.HTMLStyleElementClass
mshtml.HTMLBodyClass
mshtml.HTMLDivElementClass
June 9, 2008, 7:14 PM
laurion

Ahh, okay. Thanks a lot Mynd  ;D

I ended up stepping through the HTML code by hand and figuring out what type of HTML elements I was looking for:
[code]
private void PerformAction()
        {
            IHTMLDocument2 cDoc = (IHTMLDocument2)this.wbMain.Document.DomDocument;
           
            FramesCollection fcol = cDoc.frames;

            Object o = "main";
            IHTMLWindow2 hw = (IHTMLWindow2)fcol.item(ref o);

            HTMLDocument frameDoc = (HTMLDocument)hw.document;

            foreach (HTMLAnchorElement anchelem in frameDoc.getElementsByTagName("A"))
            {
                string strHref = anchelem.href.ToString();

                if (strHref.Contains("login"))
                {
[/code]
etc.

But I still appreciate your help. Thanks!
June 9, 2008, 11:32 PM

Search