如何使用Powershell和XPath从xml中提取值,并基于文件(如果存在或不存在)的值搜索

如何使用Powershell和XPath从xml中提取值,并基于文件(如果存在或不存在)的值搜索,xml,windows,powershell,xpath,msbuild,Xml,Windows,Powershell,Xpath,Msbuild,问题: 我有以下XML数据: 这是给我的“HintPath”值,但我无法获得匹配的“Project”值,以检查HintPath错误或文件不存在的项目 请帮忙 将XML填充到变量中 [xml]$foo = @' <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer

问题: 我有以下XML数据:

这是给我的“HintPath”值,但我无法获得匹配的“Project”值,以检查HintPath错误或文件不存在的项目


请帮忙

将XML填充到变量中

[xml]$foo = @'
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <ItemGroup>
    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>c:\packages\EntityFramework.6.1.2\lib\net45\EntityFramework.dll</HintPath>
    </Reference>
    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>c:\packages\EntityFramework.6.1.2\lib\net45\EntityFramework.SqlServer.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
  </ItemGroup>
</Project>
'@
我肯定这不是你想要的信息。这种方法正是我想展示的。我正在使用多个循环。一个是您关心的第一个实体:project。第二个是另一个:引用路径。希望这是有道理的

祝你好运,
A-

我在你的帖子中没有看到任何xpath用法。你需要做的就是查看include:
%{$\uHintPath+''+$\uInclude}
@TheIncorrigible1是的,它也给了我参考值。但是,它也给出了那些作为子元素没有“HintPath”元素的值。表示“”-->由于没有“HIntPath”元素作为它的子元素,因此不希望打印此内容。我们如何根据where子句进行过滤?方法-HintPath存在的位置或HintPath=您可以通过将管道中的
| where Object{$$.HintPath}}
放在每个对象的
前面来实现这一点-$fians+=$results.Project.ItemGroup.Reference{$.Include}选择对象-唯一的{where Object{.HintPath}抱歉,我是Powershell新手,所以我的问题可能听起来很愚蠢!根据我想获得HinPathIn PowerShell项目名称的问题,我的口号是“左过滤,右格式化”<代码>何处对象
应该是管道中的第一件事。感谢您的回答!实际上这里有一个问题。我的xml包含更多的“导入项目”标记:因此这些标记也被提取。您可以看到这些没有附加hintpath标记。@Alex How to filter==>Write Output“在$(${project}.Import.GetAttribute('project')中的错误路径”
[xml]$results = Get-Content path-to-file/MigratorConsole.csproj

$fianl += $results.Project.ItemGroup.Reference | %{$_.HintPath} | select-object -unique

$fianl
$fianl.length
[xml]$foo = @'
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <ItemGroup>
    <Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>c:\packages\EntityFramework.6.1.2\lib\net45\EntityFramework.dll</HintPath>
    </Reference>
    <Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>c:\packages\EntityFramework.6.1.2\lib\net45\EntityFramework.SqlServer.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.ComponentModel.DataAnnotations" />
  </ItemGroup>
</Project>
'@
foreach ($project in $foo.project) {
    foreach ($reference in $project.ItemGroup.Reference) {
        if ([string]::IsNullOrEmpty($reference.HintPath) -eq $false) {
            if ( $(Test-Path -Path $reference.HintPath) -eq $false ) {
                Write-Output "Bad Path in $(${project}.Import.GetAttribute('Project'))"
            }
        }
    }
}