user4951
user4951

Reputation: 33120

Why is the type of mshtml.htmldocument.body.all is just object?

say dw1 is a variable whose type is msthml.htmldocument

dw1.all type is mshtml.ihtmlelementcollection

However, dw1.body.all type is object.

Why is it so?

To be more blunt.

Why is the type of dw1.all differs from the type of dw1.body.all?

Upvotes: 0

Views: 1718

Answers (1)

Markus
Markus

Reputation: 438

I get the following

enter image description here

when the DOM loads and resolves, you'll get body.All is an HtmlElementCollection of all elements underneath the current element, as defined at https://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.all(v=vs.110).aspx

This table will help navigate this structure https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument(v=vs.110).aspx

All - Gets an instance of HtmlElementCollection, which stores all HtmlElement objects for the document.

Body - Gets the HtmlElement for the BODY tag.

Here's how you load the DOM

        // Construct DOM
        HTMLDocument doc = new HTMLDocument();

        // Obtain the document interface
        IHTMLDocument2 htmlDocument = (IHTMLDocument2)doc;
        string htmlContent = "<!DOCTYPE html><html><body><h2>An unordered HTML list</h2><ul>  <li>Coffee</li>  <li>Tea</li>  <li>Milk</li></ul></body></html>";

        // Load the DOM
        htmlDocument.write(htmlContent);

        // Extract all body elements
        IHTMLElementCollection allBody = htmlDocument.body.all;


        // All page elements including body, head, style, etc 
        IHTMLElementCollection all = htmlDocument.all;

        // Iterate all the elements and display tag names
        foreach (IHTMLElement element in allBody)
        {
            Console.WriteLine(element.tagName);
        }

Upvotes: 1

Related Questions