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

时间:2012-11-22 11:15:22

标签: c# xml xml-namespaces xmldocument

我正试图从SelectNode班级致电XmlDocument并因此错误而遇到麻烦:

  

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

我的代码:

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

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

3 个答案:

答案 0 :(得分:51)

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

答案 1 :(得分:15)

文档可以由if token != "": tokens.append(token) 遍历,并且不一定需要使用GetElementsByTagName

XmlNamespaceManager

Fiddle

答案 2 :(得分:0)

var document = new XmlDocument();
document.LoadXml(rawData);

var nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("cbc", "urn:xxx"); //for example
nsmgr.AddNamespace("cac", "urn:yyy");            

XmlElement xmlElem = document.DocumentElement;
var node = xmlElem.SelectSingleNode("cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID[@schemeID='VKN']/text()", nsmgr);
var nodeText = node.InnerText;

enter image description here

应添加将在 XML 中使用的所有命名空间。
然后您可以使用 xpath 访问相关节点的值。