【发布时间】:2014-05-29 20:00:03
【问题描述】:
我想使用 PowerShell 获取我的 csproj 文件中所有项目引用的列表。目前我有以下方法:
[xml]$csproj = Get-Content MyProject.csproj
$refs = $csproj.SelectNodes("//ProjectReference")
foreach($ref in $refs) {
# Later on output more useful information
Write-Host $ref.Name
}
然而,脚本不输出任何东西,尽管在给定的 csproj 文件中肯定有 ProjectReference 元素。以下是有效的:
[xml]$csproj = Get-Content MyProject.csproj
foreach($l in $csproj.Project.ItemGroup.ProjectReference) { Write-Host $l.Include }
但我稍后也需要 XPath + 它为每个不包含 ProjectReference 的 ItemGroup 输出错误 - 那么如何使用 SelectNodes 函数使 XPath 工作?
示例 XML(基本上任何带有项目引用的 VS csproj 文件都可以):
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup></ItemGroup>
<ItemGroup>
<ProjectReference Include="Text"></ProjectReference>
<ProjectReference Include="Text2"></ProjectReference>
</ItemGroup>
<ItemGroup></ItemGroup>
</Project>
【问题讨论】:
-
您能否添加一个提供所描述行为的示例 XML 文档?
标签: xml powershell xpath powershell-4.0