Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
DotNetBrowser 1.8.1 brings an update to the DOM API. Now it is possible to create new DOM elements and text nodes, append them as child nodes to the existing structure, insert node before another one, replace and remove an existing node.
This new functionality allows to modify the whole DOM structure or the particular part of it.
Example
The following sample demonstrates how to create a new DOM element and text node and append these new nodes as child nodes to the existing one.
C#
using DotNetBrowser; using DotNetBrowser.DOM; using DotNetBrowser.Events; using DotNetBrowser.WPF; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace DOMCreateElementSample { class Program { public class WindowMain :Window { private WPFBrowserView browserView; public WindowMain() { Browser browser = BrowserFactory.Create(); browserView = new WPFBrowserView(browser); browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e) { if (e.IsMainFrame) { DOMDocument document = e.Browser.GetDocument(); DOMNode root = document.GetElementById("root"); DOMNode textNode = document.CreateTextNode("Some text"); DOMElement paragraph = document.CreateElement("p"); paragraph.AppendChild(textNode); root.AppendChild(paragraph); } }; Content = browserView; Width = 1024; Height = 768; this.Loaded += WindowMain_Loaded; } void WindowMain_Loaded(object sender, RoutedEventArgs e) { browserView.Browser.LoadHTML("<html><body><div id='root'></div></body></html>"); } [STAThread] public static void Main() { Application app = new Application(); WindowMain wnd = new WindowMain(); app.Run(wnd); var browser = wnd.browserView.Browser; wnd.browserView.Dispose(); browser.Dispose(); } } } }
VB.NET
Imports System.Windows Imports DotNetBrowser Imports DotNetBrowser.DOM Imports DotNetBrowser.WPF Module Module1 Public Class WindowMain Inherits Window Public Dim browserView As WPFBrowserView Public Sub New() Dim browser As Browser = BrowserFactory.Create() browserView = new WPFBrowserView(browser) AddHandler browser.FinishLoadingFrameEvent, Sub(o, e) If e.IsMainFrame Then Dim document As DOMDocument = e.Browser.GetDocument() Dim root As DOMElement = document.GetElementById("root") Dim textNode As DOMNode = document.CreateTextNode("Some text") Dim paragraph As DOMElement = document.CreateElement("p") paragraph.AppendChild(textNode) root.AppendChild(paragraph) End If End sub Content = browserView Width = 1024 Height = 768 AddHandler Me.Loaded, AddressOf WindowMain_Loaded End Sub Private Sub WindowMain_Loaded(sender As Object, e As RoutedEventArgs) browserView.Browser.LoadHTML("<html><body><div id='root'></div></body></html>") End Sub End Class <STAThread> Sub Main() Dim app As Application = New Application() Dim wnd As WindowMain = New WindowMain() app.Run(wnd) Dim browser = wnd.browserView.Browser wnd.browserView.Dispose() browser.Dispose() End Sub End Module