PowerShell 解析 xml 并保存更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6037761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:42:52  来源:igfitidea点击:

PowerShell parse xml and save changes

xmlpowershell

提问by Don Rolling

I'm parsing a csproj file for a nuget install and I've got a node that needs to be altered. The node in question is the node named "Generator" where it's value equals "TextTemplatingFileGenerator" and it's parent node has an attribute of "WebConfigSettingsGeneratorScript.tt" (the second part isn't in here yet).

我正在解析用于 nuget 安装的 csproj 文件,并且我有一个需要更改的节点。有问题的节点是名为“Generator”的节点,它的值等于“TextTemplatingFileGenerator”,它的父节点有一个“WebConfigSettingsGeneratorScript.tt”属性(第二部分还没有在这里)。

Here's the script that I've got, but it's not quite done. It's working, but it saves an empty file. Also, it doesn't have the 2nd part of my where clause, which is

这是我得到的脚本,但还没有完全完成。它正在工作,但它保存了一个空文件。此外,它没有我的 where 子句的第二部分,即

$path = 'C:\Projects\Intouch\NuGetTestPackage\NuGetTestPackage'
cd $path
$files = get-childitem -recurse -filter *.csproj
foreach ($file in $files){
    ""
    "Filename: {0}" -f $($file.Name)
    "=" * ($($file.FullName.Length) + 10)   

    if($file.Name -eq 'NuGetTestPackage1.csproj'){
        $xml = gc $file.FullName | 
        Where-Object { $_.Project.ItemGroup.None.Generator -eq 'TextTemplatingFileGenerator' } | 
        ForEach-Object { $_.Project.ItemGroup.None.Generator = '' }
        Set-Content $file.FullName $xml
    }   
}

Here's a basic version of the XML:

这是 XML 的基本版本:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="T4\WebConfigSettingGeneratorScript.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>WebConfigSettingGeneratorScript.txt</LastGenOutput>
    </None>

Thanks a lot. I'm a total PowerShell n00b!

非常感谢。我是一个完整的 PowerShell n00b!

回答by Keith Hill

As @empo says, you need to cast the output of gc $file.FullNameto [xml] e.g. $xml = [xml](gc $file.FullName). And then after making the change but before looping to the next file, you need to save the file e.g. $xml.Save($file.FullName).

正如@empo 所说,您需要将输出转换gc $file.FullName为 [xml] 例如$xml = [xml](gc $file.FullName)。然后在进行更改之后但在循环到下一个文件之前,您需要保存文件,例如$xml.Save($file.FullName).

This works with the sample project you provided:

这适用于您提供的示例项目:

$file = gi .\test.csproj
$pattern = 'TextTemplatingFileGenerator'
$xml = [xml](gc $file)
$xml | Where {$_.Project.ItemGroup.None.Generator -eq $pattern} |
       Foreach {$_.Project.ItemGroup.None.Generator = ''}
$xml.Save($file.Fullname)

回答by Emiliano Poggi

Are you missing a cast?

你缺演员吗?

$xml = [xml] gc $file.FullName