将XdmNode转换为XmlNode

问题描述 投票:0回答:1

我正在研究以下内容:https://saxonica.plan.io/boards/3/topics/1468http://saxon-xslt-and-xquery-processor.13853.n7.nabble.com/C-How-to-get-original-Node-from-XdmNode-td5511.html,但无法绕开我的头。

我已经安装了Saxon-HE 9.5.1.5 NuGet程序包,并试图将XPath 2.0功能与XmlDocument挂钩。

这是我根据所阅读的内容当前设置的代码:

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

好吧,正如预期的那样(我没有使用这些类的指令),这会引发2个错误:

#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?)

我似乎无法using net.sf.saxon.om.Sequence或其他任何东西,没有针对这些的参考。

要做这项工作我必须做什么?

.net xpath xmldocument saxon xmlnode
1个回答
1
投票

如果要使用在saxon9he.dll中而不是在saxon9api.dll中定义的类,例如VirtualNode或Sequence,那么您将需要添加using指令,并在项目中添加对此DLL的依赖项。

有关这些类的文档,您必须转到Java文档。该DLL是使用与Saxon-HE / J产品相同的Java源代码构建的,因此Javadoc文档均适用。您需要了解基本类型(例如boolean / string / int)的映射,但这是显而易见的。 Visual Studio中的对象浏览器将以.NET术语显示这些类/方法的签名。

我希望Saxon提供一种无需使用此类即可直接从XdmNode到达基础XmlNode的方法,但是查看文档似乎并不存在。这似乎是一个疏忽,我会看看是否可以添加它。

== 2020更新==

有关最新信息,请参见How can I convert a Saxon XdmNode to an XmlDocument?

© www.soinside.com 2019 - 2024. All rights reserved.