将 XMLDocument 设置为 Saxon.Api.XMLDestination

Set XMLDocument as Saxon.Api.XMLDestination

我正在尝试从 XSLTCompiledTransformation 切换到 Saxon XSLTTransformation,但我在将输出目标指定为 XML 文档到 saxon xslt 转换时遇到问题。

这是用于执行 xslt 转换的旧代码:

XsltCompiledTransform transform = ... load xslt transform
XsltArgumentList arguments = ... load arguments
var result = new XmlDocument();

using(var xmlReader = new XmlNodeReader(inputXml)) {
    using(var writer = result.CreateNavigator().AppendChild())
        transform.Transform(xmlReader, arguments, writer);
}

我尝试重写它以使用 Saxon 库,但输出 xmlDocument 时遇到问题:

XsltTransformer transform = ... load xslt transform
var result = new XmlDocument();

Processor processor = new Processor()
XdmNode input = pro.NewDocumentBuilder().Wrap(inputXml);
transform.InitialContextNode = input;

//tried specifying result as XMLDestination:
transform.Run(result); //getting error

//also tried this:
using(var writer = result.CreateNavigator().AppendChild())
    transform.Run(writer);

如何将 XMLDocument 转换为 XmlDestination 或者如何设置目标,然后将该结果转换回 XMLDocument?

使用 DOMDestination http://saxonica.com/html/documentation9.6/dotnetdoc/Saxon/Api/DomDestination.html#DomDestination%28%29,例如

var result = new DOMDestination();
transform.Run(result);
var resultDoc = result.XmlDocument; // now resultDoc is an XmlDocument

或者作为替代方案

var result = new XmlDocument();
transform.Run(new DOMDestination(result));