提供了XmlNamespaceManager,但仍然需要“Namespace Manager或XsltContext”

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

我正在尝试阅读以下内容并在其中选择一个节点

<ns1:OrderInfo xmlns:ns1="http://xxxxxx Some URL XXXX">
   <pricing someAttrHere>
      <childnodes>
   </pricing>
</ns1:OrderInfo>

.

XmlDocument document = new XmlDocument();
document.Load(Server.MapPath("order.xml"));

XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace("ns1", "http://xxxxxx Some URL XXXX");
query = "/ns1:OrderInfo/pricing";
XmlNodeList nodeList = document.SelectNodes(query);

但它总是给“需要命名空间管理器或XsltContext”

正如你在上面看到的,我使用XmlNamespaceManager添加命名空间,但仍然给出错误,请帮助

c# .net xml xpath
1个回答
37
投票

您还需要使用XmlNamespaceManager:

XmlNodeList nodeList = document.SelectNodes(query, manager); 
© www.soinside.com 2019 - 2024. All rights reserved.