【发布时间】:2014-06-11 08:29:46
【问题描述】:
我正在尝试使用root.SelectNodes() 和XPath 选择所有节点。如需参考,请参阅msdn-documentation。
在following文档解释中,也可以搜索包含属性的节点(如果这确实是理解错误,请指正)。
所以我使用了下面这行代码:
XmlNodeList nodes = projectDoc.DocumentElement.SelectNodes("descendant::Compile[attribute::Include]");
我正在尝试读取以下数据:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<ItemGroup>
<Compile Include="ArrayExtensions.cs" />
<Compile Include="ArrayIterator.cs" />
<Compile Include="AutoInitializeAttribute.cs" />
<Compile Include="AutoInitializePriority.cs" />
<Compile Include="AutoRegisterAttribute.cs" />
<Compile Include="FormattableExtensions.cs" />
<Compile Include="Mathematics\PrimeNumbers.cs" />
</ItemGroup>
</Project>
如上面的代码示例所示,我想获取所有包含包含属性的 XmlNode。但是,当我执行我的代码时,nodes 包含 0 个元素。
我在这里做错了什么?
【问题讨论】:
-
我强烈怀疑问题出在命名空间上——您实际上应该在
http://schemas.microsoft.com/developer/msbuild/2003命名空间中寻找Compile元素。您是否有任何理由需要在 XPath 中执行此操作?使用 LINQ to XML,这将是微不足道的。 -
没有特别的原因,如果您有更好的解决方案,将不胜感激!另外,我提供的问题的解决方案也很棒,仅用于学习目的。