How to: Access the HTML Source in the Managed HTML Document Object Model
2008-11-15 21:36
597 查看
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属性来获得。
下面的代码是个示例:
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.
Create a second Form in your application called CodeForm.
Add a RichTextBox control to CodeForm and set its Dock property to Fill.
Create a public property on CodeForm called Code
public string Code
{
get
{
if (richTextBox1.Text != null)
{
return (richTextBox1.Text);
}
else
{
return ("");
}
}
set
{
richTextBox1.Text = value;
}
}
Add a Button control named Button1 to your Form, and monitor for the Click event. For details on monitoring events, see Consuming Events.
Add the following code to the Click event handler.
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();
}
}
}
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
Create a new Windows Forms application. Start with a single Form, and call it Form1.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.
Create a second Form in your application called CodeForm.
Add a RichTextBox control to CodeForm and set its Dock property to Fill.
Create a public property on CodeForm called Code
public string Code
{
get
{
if (richTextBox1.Text != null)
{
return (richTextBox1.Text);
}
else
{
return ("");
}
}
set
{
richTextBox1.Text = value;
}
}
Add a Button control named Button1 to your Form, and monitor for the Click event. For details on monitoring events, see Consuming Events.
Add the following code to the Click event handler.
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();
}
}
}
相关文章推荐
- How to Access the DOM(Document Object Model) in MOSS 2007
- Common Programming Tasks in the JavaScript Object Model
- How to access the keys in dictionary in object-c
- You cannot access the client's file system using the FileSystemObject in your ASP code
- cannot expose session attribute '_from_source' because of an existing model object of the same name
- javascript: The Document Object Model(DOM)
- 了解dom:DOM全称为The Document Object Model
- How to get the HTML source of a webpage in Ruby
- Decoupling models from the database: Data Access Object pattern in PHP
- How to access the properties of an object in Javascript
- (3)选择元素——(2)文档对象模型(The Document Object Model)
- Gaining Access to the Spring Context in Non Spring Managed Classes
- Using the HTMLControl Class in Adobe AIR to parse HTML as a data source
- 如何解决Selenium中"Cannot find function addEventListener in object [object HTMLDocument]"的错误
- PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the Eve
- How to: Create a Business Model in the XPO Data Model Designer
- Inside The C++ Object Model(Note):sizeof in an object
- Part 29 - Using data transfer object as the model in mvc
- 转:Best Practices: Common Coding Issues When Using the SharePoint Object Model
- Inside the C++ Object Model 深度探索对象模型 5-Construction 6-Runtime 7-Object