gpt4 book ai didi

c# - 当标记包含 xmlNamespace 时,SelectSingleNode 返回 null

转载 作者:IT王子 更新时间:2023-10-29 03:54:26 26 4
gpt4 key购买 nike

我正在将一个字符串加载到包含以下结构的 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

当我从根元素 (Project) 中删除 xmlns 属性时,它工作正常。

如何让 SelectSingleNode 返回相关元素?

最佳答案

你应该使用 XmlNamespaceManager在您调用 SelectSingleNode() 的电话中:

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

关于c# - 当标记包含 xmlNamespace 时,SelectSingleNode 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4171451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com