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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13511527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:49:55  来源:igfitidea点击:

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

c#xmlxml-namespacesxmldocument

提问by rizwan ansari

I am trying to call SelectNodefrom XmlDocumentclass and trouble due to this error:

由于此错误,我正在尝试SelectNodeXmlDocument课堂上拨打电话并遇到麻烦:

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

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

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:

<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>

采纳答案by Ria

You have to add xslnamespace to XmlNamespaceManager:

您必须将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);

回答by Daniel B

The document can be traversed by GetElementsByTagNameand it doesn't necessarily need using XmlNamespaceManager:

文档可以被遍历GetElementsByTagName并且不一定需要使用XmlNamespaceManager

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

Fiddle

小提琴