当标签包含 xmlNamespace 时,SelectSingleNode 返回 null

发布于 2024-10-02 09:45:15 字数 730 浏览 1 评论 0原文

我正在将字符串加载到包含以下结构的 XML 文档中:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">                  
  <ItemGroup>
    <Compile Include="clsWorker.cs" />        
  </ItemGroup>      
</Project>

然后将所有内容加载到 XmlDocument 中:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);

然后出现以下问题:

XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // returns null

当我删除 xmlns 时> 来自根元素(Project)的属性,它工作正常。

如何让 SelectSingleNode 返回相关元素?

I'm loading a string into an XML document that contains the following structure:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">                  
  <ItemGroup>
    <Compile Include="clsWorker.cs" />        
  </ItemGroup>      
</Project>

Then I'm loading all into an XmlDocument:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);

Then the following problem occurs:

XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // returns null

When I remove the xmlns attribute from the root element (Project), it works fine.

How do I get SelectSingleNode to return the relevant element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光磨忆 2024-10-09 09:45:15

您应该在调用 SelectSingleNode()

XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode node = xmldoc.SelectSingleNode("//msbld:Compile", ns);

You should use an XmlNamespaceManager in your call to SelectSingleNode():

XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode node = xmldoc.SelectSingleNode("//msbld:Compile", ns);
转瞬即逝 2024-10-09 09:45:15

直接取自 MSDN 上的 SelectSingleNode() 文档

注意
如果 XPath 表达式不包含前缀,则假定
命名空间 URI 是空命名空间。 如果您的 XML 包含默认值
命名空间,您仍然必须添加前缀和命名空间 URI
Xml命名空间管理器;否则,您将无法选择节点。

有关详细信息,请参阅使用 XPath 导航选择节点

接下来的示例代码是

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);

as 如果 这个 隐藏 知识"。 ;-)

Taken right from the documentation of SelectSingleNode() on the MSDN:

Note
If the XPath expression does not include a prefix, it is assumed that the
namespace URI is the empty namespace. If your XML includes a default
namespace, you must still add a prefix and namespace URI to the
XmlNamespaceManager; otherwise, you will not get a node selected.
For
more information, see Select Nodes Using XPath Navigation.

And the immediately following sample code is

XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XmlNode book = doc.SelectSingleNode("//ab:book", nsmgr);

It's not as if this would be "hidden knowledge". ;-)

心头的小情儿 2024-10-09 09:45:15

这样你就不需要指定名称空间:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("your xml");
XmlNode node = xmlDoc.SelectSingleNode("/*[local-name() = 'Compile']");
XmlNode nodeToImport = xmlDoc2.ImportNode(node, true);
xmlDoc2.AppendChild(nodeToImport);

This way you don't need to specify namespace:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("your xml");
XmlNode node = xmlDoc.SelectSingleNode("/*[local-name() = 'Compile']");
XmlNode nodeToImport = xmlDoc2.ImportNode(node, true);
xmlDoc2.AppendChild(nodeToImport);
婴鹅 2024-10-09 09:45:15

由于“ItemGroup”可能有多个“Compile”子项,并且您特别需要“Project/ItemGroup”的“Compile”子项,因此以下命令将返回所有所需的“Compile”子项,而不返回其他子项:

XmlDocument projectDoc = new XmlDocument();
projectDoc.Load(projectDocPath);
XmlNamespaceManager ns = new XmlNamespaceManager(projectDoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNodeList xnList = projectDoc.SelectNodes(@"/msbld:Project/msbld:ItemGroup/msbld:Compile", ns);

请注意,“msbld” :' 命名空间规范需要位于每个节点级别之前。

Since the 'ItemGroup' may have multiple 'Compile' children, and you specifically want the 'Compile' children of 'Project/ItemGroup', the following will return all of the desired 'Compile' children and no others:

XmlDocument projectDoc = new XmlDocument();
projectDoc.Load(projectDocPath);
XmlNamespaceManager ns = new XmlNamespaceManager(projectDoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNodeList xnList = projectDoc.SelectNodes(@"/msbld:Project/msbld:ItemGroup/msbld:Compile", ns);

Note that the 'msbld:' namespace specification needs to precede each node level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文