How to: Access the HTML Source in the Managed HTML Document Object Model

來源:互聯網
上載者:User

 How to: Access the HTML Source in the Managed HTML Document Object Model

 

The DocumentStream and DocumentText properties on the WebBrowser control return the HTML of the current document as it existed when it was first displayed. However, if you modify the page using method and property calls such as AppendChild and InnerHtml, these changes will not appear when you call DocumentStream and DocumentText. To obtain the most up-to-date HTML source for the DOM, you must call the OuterHtml property on the HTML element.

The following procedure shows how to retrieve the dynamic source and display it in a separate shortcut menu.

 

如何在託管HTML 文件物件模型裡訪問 HTML 源碼

WebBrowser控制項的DocumentStream和DocumentText屬性,包含當前文檔的HTML源碼,這些源碼是網頁一開始顯示時留下的。當你對網頁進行了修改,比如用AppendChild方法進行修改或者改變了InnerHtml屬性,這些變化將不會在DocumentStream和DocumentText這兩個屬性裡體現出來,它們仍然是最初的狀態。要獲得最新的HTML原始碼,可以訪問OuterHtml屬性來獲得。

 

下面的代碼是個樣本:

Retrieving the dynamic source with the OuterHtml property
  1. Create a new Windows Forms application. Start with a single Form, and call it Form1.

  2. Host the WebBrowser control in your Windows Forms application, and name it WebBrowser1. For more information, see How to: Add Web Browser Capabilities to a Windows Forms Application.
  3. Create a second Form in your application called CodeForm.

  4. Add a RichTextBox control to CodeForm and set its Dock property to Fill.

  5. Create a public property on CodeForm called Code

  6.  public string Code
            {
                get
                {
                    if (richTextBox1.Text != null)
                    {
                        return (richTextBox1.Text);
                    }
                    else
                    {
                        return ("");
                    }
                }
                set
                {
                    richTextBox1.Text = value;
                }
            }

  7. Add a Button control named Button1 to your Form, and monitor for the Click event. For details on monitoring events, see Consuming Events.

  8. Add the following code to the Click event handler.

  9.   private void button1_Click(object sender, EventArgs e)
            {
                HtmlElement elem;

                if (webBrowser1.Document != null)
                {
                    CodeForm cf = new CodeForm();
                    HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("HTML");
                    if (elems.Count == 1)
                    {
                        elem = elems[0];
                        cf.Code = elem.OuterHtml;
                        cf.Show();
                    }
                }
            }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.