用于更改 csproj 文件中值的 Powershell 脚本
Powershell script to change value in csproj file
我想更改下面 test.csproj 文件中 ProductVersion 标签的值。我只需要将第一次出现的 ProductVersion: 8A-0V-W3 的值更改为 A0-B0-C0
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
<ProductVersion>8A-0V-W3</ProductVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
<ProductVersion>PK-0X-SD</ProductVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
<DebugType>none</DebugType>
<ProductVersion>SD-AA-SW</ProductVersion>
</PropertyGroup>
我想出了以下命令,但它删除了所有出现的标签。有没有办法只删除第一次出现,然后将更新后的标签插入同一位置
get-content ./test.csproj | select-string -pattern 'ProductVersion' -notmatch | Out-File ./test1.csproj
使用 Select-Xml
cmdlet:
$firstVersion = (
Select-Xml //ns:ProductVersion test.csproj -Namespace @{ ns='http://schemas.microsoft.com/developer/msbuild/2003' }
)[0].Node
$firstVersion.InnerText = 'A0-B0-C0'
$firstVersion.OwnerDocument.Save((Join-Path $PWD.ProviderPath test1.csproj))
$oldValue = "8A-0V-W3";
$newValue = "A0-B0-C0";
$projFile = "./test.csproj";
$config = (Get-Content $projFile) -as [Xml];
$ns = New-Object System.Xml.XmlNamespaceManager($config.NameTable);
$ns.AddNamespace("cs", $config.DocumentElement.NamespaceURI);
$config.DocumentElement.SelectNodes("//cs:ProductVersion", $ns) | % {
$node = $_;
if ($node.InnerText -ieq $oldValue) {
$node.InnerText = $newValue;
}
}
$config.Save($projFile);
我想更改下面 test.csproj 文件中 ProductVersion 标签的值。我只需要将第一次出现的 ProductVersion: 8A-0V-W3 的值更改为 A0-B0-C0
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">iPhoneSimulator</Platform>
<ProductVersion>8A-0V-W3</ProductVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|iPhoneSimulator' ">
<DebugSymbols>true</DebugSymbols>
<ProductVersion>PK-0X-SD</ProductVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|iPhoneSimulator' ">
<DebugType>none</DebugType>
<ProductVersion>SD-AA-SW</ProductVersion>
</PropertyGroup>
我想出了以下命令,但它删除了所有出现的标签。有没有办法只删除第一次出现,然后将更新后的标签插入同一位置
get-content ./test.csproj | select-string -pattern 'ProductVersion' -notmatch | Out-File ./test1.csproj
使用 Select-Xml
cmdlet:
$firstVersion = (
Select-Xml //ns:ProductVersion test.csproj -Namespace @{ ns='http://schemas.microsoft.com/developer/msbuild/2003' }
)[0].Node
$firstVersion.InnerText = 'A0-B0-C0'
$firstVersion.OwnerDocument.Save((Join-Path $PWD.ProviderPath test1.csproj))
$oldValue = "8A-0V-W3";
$newValue = "A0-B0-C0";
$projFile = "./test.csproj";
$config = (Get-Content $projFile) -as [Xml];
$ns = New-Object System.Xml.XmlNamespaceManager($config.NameTable);
$ns.AddNamespace("cs", $config.DocumentElement.NamespaceURI);
$config.DocumentElement.SelectNodes("//cs:ProductVersion", $ns) | % {
$node = $_;
if ($node.InnerText -ieq $oldValue) {
$node.InnerText = $newValue;
}
}
$config.Save($projFile);