命名空间管理器或XsltContext需要。 此查询具有前缀,变量,或用户定义的函数(Names

2019-07-04 01:14发布

I am trying to call SelectNode from XmlDocument class and trouble due to this error:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

My code:

   public void Add(ref XmlDocument xmlFormat, String strName)
   {
        XmlDocument dom;
        XSLTemplate xsl = null;
        String strPath = "";
        XmlNodeList nl;
        XmlAttribute na;
        int n;

        nl = (XmlNodeList)xmlFormat.SelectNodes("//xsl:import/@href",nsm);
    }

and xsl:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="stylesheets/r_adresetiket.xsl" />
    <xsl:template match="/">
        <xsl:call-template name="retouradres">
            <xsl:with-param name="_retouradres" select="data/adresetiket/_retouradres" />
            <xsl:with-param name="minofdir" select="data/adresetiket/afzendgegevens/afzendgegevens" />
            <xsl:with-param name="checked" select="data/adresetiket/LB" />
        </xsl:call-template>
    </xsl:template>
</xsl:stylesheet>

Answer 1:

你必须添加xsl命名空间XmlNamespaceManager

var document = new XmlDocument();
document.Load(...);
var nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

var nl = document.SelectNodes("//xsl:import/@href", nsmgr);


Answer 2:

该文件可以通过遍历GetElementsByTagName ,它不一定需要使用XmlNamespaceManager

var nodes = document.GetElementsByTagName("xsl:import");
var href =  nodes[0].Attributes["href"];

小提琴



文章来源: Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function