DotNetBrowser DOM API provides functionality that allows evaluating XPath expressions. You can evaluate XPath expression in the scope ofDOMDocument or specifiedDOMNode. BothDOMDocument and DOMNode implementsSearchContext interface that provides the following methods:

  • XPathResult Evaluate(String expression)
  • XPathResult Evaluate(String expression, XPathResultType type)


Sample code:

C#

DOMDocument document = browser.GetDocument();
XPathResult result = document.Evaluate("count(//div)");

VB.NET

Dim document As DOMDocument = e.Browser.GetDocument()
Dim result As XPathResult = document.Evaluate("count(//div)")


The result of the evaluation is stored in the XPathResult object. First, you need to make sure that result is not an error and the evaluation completed successfully:

C#

if (result.IsError) {
    Console.WriteLine("Error: " + result.ErrorMessage);
    return;
}

VB.NET

If (result.IsError) Then
    Console.WriteLine("Error: " + result.ErrorMessage)
    Return
End If


Then you need to make sure that result contains expected value type (e.g. Number, Boolean, String, DOMNode) and extract the value itself:

C#

if (result.IsNumber) {
    Console.WriteLine("Result: " + result.Number);
}

VB.NET

If (result.IsNumber) Then
    Console.WriteLine("Result: " + result.Number)
End If


or


C#

if (result.IsString) 
{
    Console.WriteLine("Result: " + result.String);
}

VB.NET

If (result.IsString) Then
    Console.WriteLine("Result: " + result.String)
End If


or


C#

if (result.IsSingleNode) {
    DOMNode node = result.SingleNode();
    Console.WriteLine("Result: " + node.TextContent);
}

VB.NET

If (result.IsSingleNode) Then
    Dim node As DOMNode = result.SingleNode
    Console.WriteLine("Result: " + node.TextContent)
End If


Starting from DotNetBrowser 1.8.2 it is possible to use LINQ capabilities with the result of evaluating an XPath expression. For example, let’s extract the lists with DOMNode children from theXPathResult

C#

XPathResult resultNodes = document.Evaluate("/ html / body / div[2] / div[2] / div[1] / div[2] / div / *");
var nodesList= resultNodes.Iterator.Select(item => item.Node.Children);

VB.NET

Dim resultNodes As XPathResult = document.Evaluate("/ html / body / div[2] / div[2] / div[1] / div[2] / div / *")
Dim nodesList= resultNodes.Iterator.Select(Function(item) item.Node.Children)


Starting from DotNetBrowser 1.8.4 it is possible to obtain the XPath expression that describes the particular DOM element:

C#

var div = document.GetElementById("div1");
string xpathExpression = div.XPath;

VB.NET 

Dim div = document.GetElementById("div1")
Dim xpathExpression As String = div.XPath




Example

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.Threading.Tasks;
using System.Windows;

namespace XPathSample
{
    class Program
    {
        public class WindowMain : Window
        {
            private WPFBrowserView browserView;

            public WindowMain()
            {
                BrowserContext browserContext = BrowserContext.DefaultContext;

                Browser browser = BrowserFactory.Create(browserContext);

                browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
                {
                    if (e.IsMainFrame)
                    {
                        DOMDocument document = browser.GetDocument();
                        XPathResult result = document.Evaluate("count(//div)");
                        // If the expression is not a valid XPath expression or the document
                        // element is not available, we'll get an error.
                        if (result.IsError)
                        {
                            Console.WriteLine("Error: " + result.ErrorMessage);
                            return;
                        }

                        // Make sure that result is a number.
                        if (result.IsNumber)
                        {
                            Console.WriteLine("Result: " + result.Number);
                        }
                    }

                };

                browserView = new WPFBrowserView(browser);
                Content = browserView;

                Width = 1024;
                Height = 768;
                this.Loaded += WindowMain_Loaded;
            }

            void WindowMain_Loaded(object sender, RoutedEventArgs e)
            {
                browserView.Browser.LoadURL("http://www.teamdev.com/jxbrowser");
            }

            [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 browserContext As BrowserContext = BrowserContext.DefaultContext

            Dim browser As Browser = BrowserFactory.Create(browserContext)

            AddHandler browser.FinishLoadingFrameEvent, sub(o, e)
                If e.IsMainFrame Then
                    Dim document As DOMDocument = e.Browser.GetDocument()
                    Dim result As XPathResult = document.Evaluate("count(//div)")

                    ' If the expression is not a valid XPath expression or the document
                    ' element is not available, we'll get an error.
                    If (result.IsError) Then
                        Console.WriteLine("Error: " + result.ErrorMessage)
                        Return
                    End If
                    
                    ' Make sure that result is a number.
                    If (result.IsNumber) Then
                        Console.WriteLine("Result: " + result.Number.ToString())
                    End If

                End If
            End sub

            browserView = New WPFBrowserView(browser)

            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.LoadURL("http://www.teamdev.com/dotnetbrowser")
        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