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

时间:2019-07-05 21:13:06

标签: c# visual-studio upgrade .net-framework-version

我刚刚浏览了解决方案中的每个项目,并从.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>

如何解决此问题并使我的项目正确加载?另外,请告诉我是否需要更多信息。

1 个答案:

答案 0 :(得分:2)

实际上,我认为 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中创建一个新的控制台项目,将其卸载并向其中添加一行,然后在单击“重新加载”按钮时发生类似的问题:

enter image description here

修改项目文件时请务必小心。