问题
I have the following ps1 file. There are three problems in this file.
Q1. How to use single quote and double-quote in XML? I googled and found that I need to put one extra single quote. I tried but didn't work.
Q2. I got "wrong type" error when I append new PropertyGroup as a child to Project node. How can I fix it.
Q3. Can I append multiple PropertyGroups to Project Node?
$dir = "C:\Work\scripttest\output\"
$ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }
$configs = [xml]"<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Dev-1|AnyCPU'">
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
";
Get-ChildItem $dir *.csproj -recurse |
% {
$content = [xml](gc $_.FullName);
$project = $content.Project;
$project
$project.AppendChild($configs);
# $content.Save($_.FullName);
}
Thanks in advance!
回答1:
Q1: Escape character in powershell is ` and not quote. Keep in mind you should also escape $ symbol
Q2: You had problems because $project.AppendChild();
is XmlNode
and your $configs
is XmlDocument
Q3: You can, but not sure if MsBuild will be happy with it
And here's the script itself:
$dir = "C:\Work\scripttest\output\"
$ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }
$configs = [xml] "<PropertyGroup Condition=`"'`$(Configuration)|`$(Platform)' == 'Dev-1|AnyCPU'`">
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>"
Get-ChildItem $dir *.csproj -recurse |
% {
$content = [xml](gc $_.FullName);
$importNode = $content.ImportNode($configs.DocumentElement, $true)
$project = $content.Project;
$project
$project.AppendChild($importNode);
# $content.Save($_.FullName);
}
As you can see I had to ImportNode fisrt as it was coming from another document
来源:https://stackoverflow.com/questions/9991688/how-to-add-new-propertygroup-to-csproj-from-powershell