.NET Framework 4.7.2 升级 - 无法加载项目文件

.NET Framework 4.7.2 Upgrade - The project file could not be loaded

我刚刚检查了解决方案中的每个项目并从 .NET Framework 4.6.1 => 4.7.2 升级,但只有一个项目无法加载。当我尝试重新加载项目时,它会抛出此错误:

3:07 PM Project 'MyProject' load finished with warnings: The project file could not be loaded. Data at the root level is invalid. Line 1, position 1. at (1:1)

3:07 PM Project 'MyProjectEmailService' load failed: The project file could not be loaded. Data at the root level is invalid. Line 1, position 1.  C:\Projects\Repo\Source\MyProject\MyProjectEmailService\MyProjectEmailService.csproj at (1:1)

3:07 PM Project 'MyProjectEmailService.Tests' load finished with warnings: The project file could not be loaded. Data at the root level is invalid. Line 1, position 1. at (1:1)

3:07 PM Project 'MyProjectEmailService.Tests' load finished with warnings: The project file could not be loaded. Data at the root level is invalid. Line 1, position 1. at (1:1)

3:08 PM Project 'MyProjectEmailService' load failed: The project file could not be loaded. The 'Project' start tag on line 2 position 2 does not match the end tag of 'Target'. Line 281, position 5.  C:\Projects\Repo\Source\MyProject\MyProjectEmailService\MyProjectEmailService.csproj at (281:5)

3:09 PM Project 'MyProjectEmailService' load failed: The project file could not be loaded. The 'Project' start tag on line 2 position 2 does not match the end tag of 'Target'. Line 281, position 5.  C:\Projects\Repo\Source\MyProject\MyProjectEmailService\MyProjectEmailService.csproj at (281:5)

据我所知,MyProjectEmailService.csproj

中的第 281 行似乎有问题

在第 281 行,我找到 </target>

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\..\packages\OctoPack.3.6.1\build\OctoPack.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\OctoPack.3.6.1\build\OctoPack.targets'))" />
    <Error Condition="!Exists('..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets'))" />
    <Error Condition="!Exists('..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets'))" />
  </Target>
  <Import Project="..\..\packages\OctoPack.3.6.1\build\OctoPack.targets" Condition="Exists('..\..\packages\OctoPack.3.6.1\build\OctoPack.targets')" />
  <Import Project="..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets" Condition="Exists('..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets')" />
  <Import Project="..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets" Condition="Exists('..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets')" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

这是 csproj 文件的第一行

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

我该如何解决这个问题并让我的项目正确加载?另外,如果您需要更多信息,请告诉我。

实际上我认为 Prateek 已经找到了问题的原因。

您上面分享的内容的正确格式应该是这样的:

<Project...>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('..\..\packages\OctoPack.3.6.1\build\OctoPack.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\OctoPack.3.6.1\build\OctoPack.targets'))" />
  <Error Condition="!Exists('..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets'))" />
  <Error Condition="!Exists('..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets'))" />

  </Target> //**This line is where the error occurs**

  <Import Project="..\..\packages\OctoPack.3.6.1\build\OctoPack.targets" Condition="Exists('..\..\packages\OctoPack.3.6.1\build\OctoPack.targets')" />
  <Import Project="..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets" Condition="Exists('..\..\packages\Selenium.Firefox.WebDriver.0.23.0\build\Selenium.Firefox.WebDriver.targets')" />
  <Import Project="..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets" Condition="Exists('..\..\packages\Selenium.WebDriver.GeckoDriver.0.24.0\build\Selenium.WebDriver.GeckoDriver.targets')" />

  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

现在很明显你有一个额外的 </Target>,我假设你可能在升级前有一个自定义目标,并在升级后删除了目标。但是不小心忘了删除它的结束标签。

解法:

所以我认为删除 </Target> 行可以帮助解决这个问题。

而且这个问题是可重现的,在 VS 中创建一个新的控制台项目,卸载它并在其中添加一行然后当您单击重新加载按钮时会发生类似的问题:

修改项目文件时要小心。