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:


DOMDocument document = browser.GetDocument();
XPathResult result = 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:


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


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


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


or


C#
if (result.IsString()) {    Console.WriteLine("Result: " + result.getString());}


or


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


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


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


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


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

Example


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();
            }
        }
    }
}