MSBuild Error: The attribute "Remove" in element <ProjectReference> is unrecognized

14,898

You cannot use the Remove attribute with static Items. Static items are those declared outside of targets. You can only use this attribute inside of dynamic item declarations. Dynamic item declarations are those found inside of a target. For example take a look at the following build script.

<Project ToolsVersion="3.5" 
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <ProjectReference  Include="One.dll"/>
  </ItemGroup>
  <Target Name="Demo">
    <ItemGroup>
      <ProjectReference  Remove="@(ProjectReference)"/>
    </ItemGroup>
    <Message Text="ProjectReference : @(ProjectReference)"/>
  </Target>
</Project>

Also note that you should not use Remove="*" that will not remove everything. It will remove every file in the current directory which is contained in the ProjectReference item group. If you want to clear out an item you have to do Remove="@(ProjectReference)" where ProjectReference is the item.

Share:
14,898
smaglio81
Author by

smaglio81

.NET Programming in Santa Barbara, CA.

Updated on June 15, 2022

Comments

  • smaglio81
    smaglio81 almost 2 years

    I'm attempting to setup a .csproj file to have a conditional item group which will remove all of the elements in the <ProjectReference> item group.

    For example:

    <ItemGroup>
       <ProjectReference Include="..\..\..\..\Projects\Registrar\Ucsb.Sa.Registrar.Common\Ucsb.Sa.Registrar.Common\Ucsb.Sa.Registrar.Common.csproj">
          <Project>{1EDDDE57-0181-41B4-B2AE-FB76450F85C8}</Project>
          <Name>Ucsb.Sa.Registrar.Common</Name>
       </ProjectReference>
    </ItemGroup>
    <ItemGroup Condition="$(OnBuildServer) == 'true'">
       <ProjectReference Remove="*" />
    </ItemGroup>
    <ItemGroup Condition="$(OnBuildServer) == 'true'">
       <Reference Include="Ucsb.Sa.Registrar.Common">
          <SpecificVersion>False</SpecificVersion>
          <HintPath>$(RegCommonDll)</HintPath>
       </Reference>
    </ItemGroup>
    

    But, when I load the project into VS 2008, I get the error message 'The attribute "Remove" in element <ProjectReference> is unrecognized". The odd thing is that the Remove attribute is in the schema (C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas\1033\MSBuild\Microsoft.Build.Core.xsd). There is MSDN documentation on it (http://msdn.microsoft.com/en-us/library/bb651786.aspx). And, there is a comment about it at the bottom of the MSDN article titled "MSBuild Items".

    The .csproj file seems to be pointing to .NET 3.5; but I am unable to verify if that version of msbuild is being used to load the project (does anyone know how to do that?)

    First line of .csproj file:

    <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    

    PS. I got the idea to use the conditionals from Build with msbuild and dynamically set project references