[英]I want to create ChooseElement for .csproj with MSBuild How?
I want to create something like below for creating .csproj file programmatically, I use我想创建类似下面的东西来以编程方式创建 .csproj 文件,我使用
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.如何在 MsBuild 中创建带有任何 When Condition 和其他块的 xml 块上方的 ChooseElement。
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 !我觉得这个 xml 块太复杂了!
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();
您的代码存在问题,即您正在使用var choose = root.CreateChooseElement();
创建ProjectElement 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元素,即var root = ProjectRootElement.Create();
你缺少 root.AppendChild(choose);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.