【问题标题】:MSB4067: The element <#text> beneath element <Target> is unrecognizedMSB4067:元素 <Target> 下的元素 <#text> 无法识别
【发布时间】:2018-05-19 04:30:08
【问题描述】:

在 Visual Studio 中,我能够成功编译代码,但是当我尝试使用 MSBuild 编译代码时,它会抛出如下错误

MSB4067:元素 Target 下的元素 无法识别。

请注意:我在发布模式下使用 MSBuild 构建代码,我为 AfterTarget 添加的代码仅适用于调试模式。

如果我删除代码,即它工作正常。

这是我的 csproj 文件

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" 
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B9B35177-B239-41A9-946F-3BB1CC14BD3B}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ABC.MyProject.Engine</RootNamespace>
<AssemblyName>ABC.MyProject.Engine</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
 </PropertyGroup>
 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="ABCDataAccess, Version=2017.2.1.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\lib\internal\ABCDataAccess.dll</HintPath>
</Reference>
<Reference Include="ABCNotifier, Version=2016.2.2.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\lib\internal\ABCNotifier.dll</HintPath>
</Reference>
<Reference Include="ABCScheduler, Version=2016.2.2.0, Culture=neutral, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\lib\internal\ABCScheduler.dll</HintPath>
</Reference>
<Reference Include="log4net">
  <HintPath>..\lib\thirdparty\log4net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="core.cs" />
<Compile Include="Model\FileInstance.cs" />
<Compile Include="Model\FileLifeCycle.cs" />
<Compile Include="Model\StateExecutor.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
  <AutoGen>True</AutoGen>
  <DesignTimeSharedInput>True</DesignTimeSharedInput>
  <DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="Enterprise\Implementation\FileKarmaHandler.cs" />
<Compile Include="Enterprise\Implementation\FileKarma.cs" />
<Compile Include="Enterprise\Implementation\FileLifeCycleData.cs" />
<Compile Include="Enterprise\Interface\IFileLifeCycleData.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
  <Generator>SettingsSingleFileGenerator</Generator>
  <LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\ABC.MyProject.Core.csproj">
  <Project>{E1B4DF28-23C5-4865-B7B7-51707D2116E3}</Project>
  <Name>ABCCore</Name>
 </ProjectReference>
 </ItemGroup>
<ItemGroup>
<Content Include="Config\Archiver.xml" />
<Content Include="Config\ABClog.xml" />
<Content Include="Config\DataAccess.xml" />
<Content Include="Config\Notifier.xml" />
<Content Include="Config\Security.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>
  rmdir config
  mkdir config
  rmdir sqls
  mkdir sqls
  rmdir templates
  mkdir templates
  exit 0
</PreBuildEvent>
</PropertyGroup>

<PropertyGroup>
 <PostBuildEvent>
  copy $(ProjectDir)Config\*.xml config\
  copy $(ProjectDir)..\lib\thirdparty\*.* $(ProjectDir)\bin\$(Configuration)\
  copy $(ProjectDir)..\Core\Config\*.xml config\
  copy $(ProjectDir)..\Core\sqls\*.*  sqls\
</PostBuildEvent>
</PropertyGroup>

<UsingTask TaskName="XmlPreprocess" AssemblyFile="..\lib\msbuild\lib\XmlPreprocess.MSBuildTasks.dll" />

<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Debug' ">
 
<PropertyGroup> 
 <ConfigValuesSpreadsheetFileLocation>
  ..\lib\msbuild\config\Settings.xls
</ConfigValuesSpreadsheetFileLocation>
</PropertyGroup>
<XmlPreprocess
  ToolPath="..\lib\msbuild\lib\"
  InputFiles="..\Core\Config\AlertParams.xml"
  OutputFiles="bin\Debug\config\AlertParams.xml"
  SpreadsheetFiles="$(ConfigValuesSpreadsheetFileLocation)"
  Environment="$(Configuration)"
  Clean="true"
  Validate="true" />
 
<XmlPreprocess
  ToolPath="..\lib\msbuild\lib\"
  InputFiles="..\Core\Config\CoreParams.xml"
  OutputFiles="bin\Debug\config\CoreParams.xml"
  SpreadsheetFiles="$(ConfigValuesSpreadsheetFileLocation)"
  Environment="$(Configuration)"
  Clean="true"
  Validate="true" />
 
<XmlPreprocess
  ToolPath="..\lib\msbuild\lib\"
  InputFiles="..\Core\Config\StateParams.xml"
  OutputFiles="bin\Debug\config\StateParams.xml"
  SpreadsheetFiles="$(ConfigValuesSpreadsheetFileLocation)"
  Environment="$(Configuration)"
  Clean="true"
  Validate="true" />
 
<XmlPreprocess
  ToolPath="..\lib\msbuild\lib\"
  InputFiles="Config\Archiver.xml"
  OutputFiles="bin\Debug\config\Archiver.xml"
  SpreadsheetFiles="$(ConfigValuesSpreadsheetFileLocation)"
  Environment="$(Configuration)"
  Clean="true"
  Validate="true" />
 
<XmlPreprocess
  ToolPath="..\lib\msbuild\lib\"
  InputFiles="Config\DataAccess.xml"
  OutputFiles="bin\Debug\config\DataAccess.xml"
  SpreadsheetFiles="$(ConfigValuesSpreadsheetFileLocation)"
  Environment="$(Configuration)"
  Clean="true"
  Validate="true" />
 
<XmlPreprocess
  ToolPath="..\lib\msbuild\lib\"
  InputFiles="app.config"
  OutputFiles="bin\Debug\app.config"
  SpreadsheetFiles="$(ConfigValuesSpreadsheetFileLocation)"
  Environment="$(Configuration)"
  Clean="true"
  Validate="true" />

  </Target>

</Project>

【问题讨论】:

  • 您使用的是哪个 msbuild 版本?
  • 4.0,不知怎么就自动开始工作了,不知道是什么问题。

标签: visual-studio msbuild


【解决方案1】:

我已经看到当 XML 格式不正确时会发生此错误,例如具有重复的 > 字符。 要解决它,您可以从您的 csproj 中删除 XML 节点,直到错误消失(它应该在构建开始时)并重复直到您缩小到 XML 问题的特定行。

【讨论】:

    【解决方案2】:

    MSB4067:元素下的元素 无法识别

    首先,您应该使用来自 Visual Studio 而不是来自 .Net Framework 的 MSBuild。

    因为从 Visual Studio 2013 开始,2013 版 MSBuild 将作为 Visual Studio 的一部分而不是 .NET Framework 提供。详情请查看this blog。因此,您应该使用来自以下位置的 MSBuild:

    C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe
    

    其次,在发布模式下通过MSBuild构建项目时,需要提供配置参数/P:configuration=Release,命令如下:

    msbuild "YourProjectPath.csproj" /P:configuration=Release
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      注意源代码管理中的合并/冲突标记。我刚刚遇到了导致此错误的 GIT 标记。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-20
        • 2023-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-25
        相关资源
        最近更新 更多