I want to create something like below for creating .csproj file programmatically, I use
for example :
<Choose>
<When Condition=" '$(Configuration)'=='debug' ">
<PropertyGroup>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>.\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="UnitTesting\*.cs" />
<Reference Include="NUnit.dll" />
</ItemGroup>
</When>
<When Condition=" '$(Configuration)'=='retail' ">
<PropertyGroup>
<DebugSymbols>false</DebugSymbols>
<Optimize>true</Optimize>
<OutputPath>.\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<OutputPath>.\bin\$(Configuration)\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
</Otherwise>
</Choose>
How can I create ChooseElement in MsBuild that create above xml block with any When Condition and Otherwise block.
I wrote a code like this but does not work :
var root = ProjectRootElement.Create();
var choose = root.CreateChooseElement();
var when1 = root.CreateWhenElement("Condition1"); // How add tag for this one ?
var when2 = root.CreateWhenElement("Condition2");
var when3 = root.CreateWhenElement("Condition3");
var when4 = root.CreateWhenElement("Condition4");
var ow = root.CreateOtherwiseElement(); // How add tag for this one ?
choose.AppendChild(when1); // Exception Here !
choose.AppendChild(when2);
choose.AppendChild(when3);
choose.AppendChild(when4);
choose.AppendChild(ow);
root.Save("test.csproj");
Exception :
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Build.dll
Additional information: The parent node is not itself parented.
I think this xml block is so complex !
Please anyone guide me.
I know its bit late. Just now saw this question. Sorry for the delay.
Problem with your code is you are creating a ProjectElement using var choose = root.CreateChooseElement();
but before appending anything into this element you should append element itself to the Root element ie., var root = ProjectRootElement.Create();
你缺少 root.AppendChild(choose);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.