Search code examples
c#visual-studioupgrade.net-framework-version

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


I just went through every project in the solution and upgraded from .NET Framework 4.6.1 => 4.7.2 and only one project cannot be loaded. When I try to reload the project, it throws this error:

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)

From what I can tell, it appears that there is something wrong with line 281 in the MyProjectEmailService.csproj

On line 281, I find </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>

Here's the first lines of the csproj file

<?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>

How can I fix this and get my project to load properly? Also, just let me know if you need more information.


Solution

  • Actually i think Prateek has located the cause of the issue.

    The correct format of the content you shared above should be like:

    <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>
    

    Now it's clear you have an extra </Target>, I assume you may have a custom target before the upgrade, and delete the target after your upgrade. But accidentally forget to delete the end tag of it.

    Solution:

    So I think deleting the </Target> line can help resolve this issue.

    And this issue is reproducible, create a new console project in VS, unload it and add a line into it then similar issue happens when you click reload button:

    enter image description here

    Just be careful when modifying the project files.