0

I want to create something like below for creating .csproj file programmatically, I use

  • Namespace: Microsoft.Build.Construction
  • Assembly: Microsoft.Build (in Microsoft.Build.dll)

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.

2 Answers2

0

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();

sree
  • 1
  • 1
  • 5
0

You're missing root.AppendChild(choose);

GGDev
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 30 '21 at 23:30