I'm digging through these: https://saxonica.plan.io/boards/3/topics/1468, http://saxon-xslt-and-xquery-processor.13853.n7.nabble.com/C-How-to-get-original-Node-from-XdmNode-td5511.html, but cannot wrap my head around.
I have installed Saxon-HE 9.5.1.5 NuGet package, and am trying to hook the XPath 2.0 features together with XmlDocument.
This is the code I have currently set up based on what I've read:
using Saxon.Api;
using System;
using System.Xml;
namespace MainTest
{
class Program
{
static void Main(string[] args)
{
SaxonFromXMLDoc();
}
private static void SaxonFromXMLDoc()
{
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
path += System.IO.Path.DirectorySeparatorChar;
// Create an XML document.
XmlDocument xmldocument = new XmlDocument();
xmldocument.Load(path + "test.html");
// Create a Saxon processor.
Processor sxp = new Processor();
// Load the source document.
XdmNode document = sxp.NewDocumentBuilder().Wrap(xmldocument);
// XPath.
XPathCompiler xpath = sxp.NewXPathCompiler();
xpath.Caching = true;
// Query for items.
foreach (XdmNode item in xpath.Evaluate("descendant-or-self::*/attribute::*[matches(name(), \"^dx\")]", document))
{
// Get an XmlNode for manipulation.
XmlNode xmlnode = (XmlNode)((VirtualNode)item.Unwrap()).getUnderlyingNode();
Console.WriteLine(xmlnode.OuterXml);
}
Console.ReadKey();
}
}
}
Well, as expected (I have no using directives for those classes) this throws 2 errors:
#1: The type 'net.sf.saxon.om.Sequence' is defined in an assembly that is not referenced. You must add a reference to assembly 'saxon9he, Version=9.5.1.5, Culture=neutral, PublicKeyToken=e1fdd002d5083fe6'.
#2: The type or namespace name 'VirtualNode' could not be found (are you missing a using directive or an assembly reference?)
I cannot seem to be able to using net.sf.saxon.om.Sequence
or anything else, there are no references for those.
What I must do to make this work?
If you want to use a class such as VirtualNode or Sequence that is defined in saxon9he.dll rather than in saxon9api.dll, then you will need to add a using directive and add a dependency on this DLL to your project.
For the documentation of these classes, you'll have to go to the Java documentation. This DLL is built from the same Java source code as the Saxon-HE/J product, so the Javadoc documentation is all applicable; yo'll need to understand the mapping of basic types such as boolean/string/int, but it's fairly obvious. The object browser in Visual Studio will display the signature of these classes/mehods in .NET terms.
I would have expected Saxon to provide a method to get straight from the XdmNode to the underlying XmlNode without using such classes, but looking at the documentation it doesn't seem to exist. It seems to be an oversight and I'll see if we can add it.
== 2020 UPDATE ==
For updated information see How can I convert a Saxon XdmNode to an XmlDocument?