Summary:
This walkthrough demonstrates how to use the Microsoft Web browser control and the Microsoft Document Object Model (DOM) to programmatically access the elements of any Web page. (3 pages)

To access the DOM programmatically, you import both the Web browser component and references to the methods, properties, and events of the DOM into your C# project. You direct the Web browser to a URL by calling its Navigate method, and you must then wait for the documentation complete event. You obtain the document by casting the Web browser Document property to an IHTMLDocument2 interface object. You can query this object for its collections, such as its link or image collections, which are returned as IHTMLElementCollection objects.

In this walkthrough, you will use the Web browser and DOM to obtain and display all anchors found in a Web page.

To access the DOM programmatically

  1. Create a new Visual C# Windows Application project named DOM.

    The form name defaults to Form1.

  2. In Solution Explorer, right-click the References folder and select Add Reference.

    The Add Reference dialog box opens.

  3. Click on the .NET tab and double-click the component named Microsoft.mshtml.
  4. Click OK.

    References to the methods, events, and properties of the Microsoft DOM are added to the project.

  5. Open the Toolbox, right-click any tool, and choose Customize Toolbox.

    The Customize Toolbox dialog box opens.

  6. Click on the COM Components tab and check Microsoft Web Browser.

    The Web browser control labeled Explorer is added to the Toolbox components.

  7. Select the Explorer component and click the open form.

    A Web browser component named axWebBrowser1 is added to the form.

  8. Add a TextBox component above the browser component and a ListBox component below it, accepting the default names of textBox1 and listBox1.
  9. Add a Button component to the right of listBox1. Change the Text property to "Submit," and accept the default name of button1.

    The resulting form should look similar to the following screen shot:

     MSDN: Accessing the DHTML DOM from C#

  10. Double-click on button1.

    The button1_Click method is added to the project.

  11. Replace the body of the button1_Click method with the following bold code:
    MSDN: Accessing the DHTML DOM from C#private void button1_Click(object sender, System.EventArgs e)
    {
    MSDN: Accessing the DHTML DOM from C#   
    object Zero = 0;
    MSDN: Accessing the DHTML DOM from C#   
    object EmptyString = "";
    MSDN: Accessing the DHTML DOM from C#   axWebBrowser1.Navigate(textBox1.Text,
    MSDN: Accessing the DHTML DOM from C#   
    ref Zero, ref EmptyString, ref EmptyString, ref EmptyString);
    MSDN: Accessing the DHTML DOM from C#}
  12. Return to the form designer, select the browser component, and click the Events icon in the Properties window.

    A list of Web browser events appears.

  13. Double-click the Document Complete event.

    The axWebBrowser1_DocumentComplete event handler is added to the project.

  14. Add the following bold line of code to the beginning of the file Form1.cs:
    MSDN: Accessing the DHTML DOM from C#using System.Data;
    MSDN: Accessing the DHTML DOM from C#
    using mshtml;
  15. Replace the body of the axWebBrowser1_DocumentComplete event handler with the following code:
    MSDN: Accessing the DHTML DOM from C#private void axWebBrowser1_DocumentComplete(
    MSDN: Accessing the DHTML DOM from C#   
    object sender, 
    MSDN: Accessing the DHTML DOM from C#   AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
    {
    MSDN: Accessing the DHTML DOM from C#   IHTMLDocument2 HTMLDocument 
    = 
    MSDN: Accessing the DHTML DOM from C#      (IHTMLDocument2) axWebBrowser1.Document;
    MSDN: Accessing the DHTML DOM from C#   IHTMLElementCollection links 
    = HTMLDocument.links;
    MSDN: Accessing the DHTML DOM from C#
    MSDN: Accessing the DHTML DOM from C#   listBox1.Items.Clear();
    MSDN: Accessing the DHTML DOM from C#
    MSDN: Accessing the DHTML DOM from C#   
    foreach (HTMLAnchorElementClass el in links)
    {
    MSDN: Accessing the DHTML DOM from C#      listBox1.Items.Add(el.outerHTML);
    MSDN: Accessing the DHTML DOM from C#   }

    MSDN: Accessing the DHTML DOM from C#}
  16. Press F5 to build and run the project.

    The Form1 application appears.

  17. Type a URL—such as www.msn.com—into the text box and click Submit.

    The Web page is displayed in the browser, and a list of its anchors appears in the list box, as shown in the following screen shot.

     MSDN: Accessing the DHTML DOM from C#

For more information, see the following topics in the MSDN Library:

相关文章:

  • 2021-08-22
  • 2021-10-06
  • 2022-02-23
  • 2021-07-21
  • 2018-07-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-04
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
相关资源
相似解决方案