简体   繁体   English

ASP.NET - 在 VS 2017 中浏览 Nuget 包时出现“根级别的数据无效”错误

[英]ASP.NET - "Data at the root level is invalid" error while browsing Nuget Packages in VS 2017

I'm trying to create a .NET Web API Application and install EntityFramework using Nuget Package Manager.我正在尝试创建一个 .NET Web API 应用程序并使用 Nuget 包管理器安装 EntityFramework。

But when I'm going to : Right Click on Project References > Manage Nuget Packages > Browse I can't find Microsoft.EntityFrameworkCore in the list.但是当我要:右键单击项目引用 > 管理 Nuget 包 > 浏览时,我在列表中找不到Microsoft.EntityFrameworkCore

Then I'm having this error showing at the bottom, in the output section :然后我在输出部分的底部显示此错误:

[nuget.org] Data at the root level is invalid. Line 1, position 1.

I use VS 2017 15.5.2 and .Net Framework 4.7.02556 .我使用VS 2017 15.5.2.Net Framework 4.7.02556

I also get this error when using the dotnet cli command dotnet add package Microsoft.EntityFrameworkCore :使用dotnet cli命令dotnet add package Microsoft.EntityFrameworkCore时,我也收到此错误:

error: The content at 'https://api.nuget.org/v3/index.json/FindPackagesById()?id='Microsoft.EntityFrameworkCore'' is not valid XML. error: Data at the root level is invalid. Line 1, position 1.

It looks like that the dotnet cli is trying to load the remote index.json file as a XML file.看起来 dotnet cli 正在尝试将远程 index.json 文件加载为 XML 文件。

For your information, dotnet --version is showing 2.1.3 .供您参考, dotnet --version显示2.1.3

Any idea?任何的想法?

ASP.NET - “Data at the root level is invalid” error while browsing Nuget Packages in VS 2017 ASP.NET - 在 VS 2017 中浏览 Nuget 包时出现“根级别的数据无效”错误

According to the error log,根据错误日志,

Data at the root level is invalid.根级别的数据无效。 Line 1, position 1.第 1 行,位置 1。

It looks like an error with XML formatting in your project, such as, web.config or NuGet.Config (Path:%appdata%\\NuGet).它看起来像是项目中的 XML 格式错误,例如web.configNuGet.Config (Path:%appdata%\\NuGet)。 So please check your xml file for any anomalies, remove everything before <?xml version="1.0" encoding="utf-8"?> .所以请检查您的 xml 文件是否有任何异常,删除<?xml version="1.0" encoding="utf-8"?>

If you still could not find any anomalies in your project, please create a new blank project without any modification, then Right Click on Project References > Manage Nuget Packages > Browse, to find the package Microsoft.EntityFrameworkCore .如果您的项目中仍然没有发现任何异常,请创建一个新的空白项目,不做任何修改,然后右键单击 Project References > Manage Nuget Packages > Browse,找到包Microsoft.EntityFrameworkCore

As test, I created aa .NET Web API Application project, changed the first line in the web.config file from:作为测试,我创建了一个 .NET Web API 应用程序项目,将 web.config 文件中的第一行从:

`<?xml version="1.0" encoding="utf-8"?>`

To:至:

zzxml version="1.0" encoding="utf-8"?>

Then open Package Manager Console, install some package with command line, I will get that error:然后打开包管理器控制台,用命令行安装一些包,我会得到那个错误:

在此处输入图片说明

Using VS 2019, my solution was to Clear All NuGet Cache(s) .使用 VS 2019,我的解决方案是Clear All NuGet Cache(s)

This can be achieved through Options -> NuGet Package Manager -> General -> Clear All NuGet Cache(s).这可以通过选项 -> NuGet 包管理器 -> 常规 -> 清除所有 NuGet 缓存来实现。

It looks like the error came from the %appdata%/Roaming/NuGet/NuGet.Config file There was no syntax error in this file but I just deleted it and reopened VS 2017 that created another NuGet.Config with less data than the deleted one.看起来错误来自%appdata%/Roaming/NuGet/NuGet.Config文件此文件中没有语法错误,但我只是删除了它并重新打开了 VS 2017,它创建了另一个NuGet.Config ,其数据少于已删除的数据. Then the error disappeared.然后错误消失了。

我刚刚删除了文件 c:\\Users{username}\\AppData\\Roaming\\NuGet\\NuGet.Config 并且 Visual Studio 2015 再次与 nuget.org 一起工作。

I had a trailing semicolon on my package source.我的包源上有一个尾随分号。 Removing that, and then restarting Visual Studio fixed the problem.删除它,然后重新启动 Visual Studio 解决了问题。 I had a custom NuGet.Config file defined in the same folder as my *.sln , but this also would cause issues on your %appdata%/NuGet/NuGet.Config path too.我在与*.sln相同的文件夹中定义了一个自定义NuGet.Config文件,但这也会导致%appdata%/NuGet/NuGet.Config路径出现问题。

包源的尾随分号

I've seen this happen before when hidden characters get into the file.. Editing this file with bitbucket online editor, you can see the red dots?我以前见过这种情况,当隐藏字符进入文件时。。用bitbucket在线编辑器编辑这个文件,你能看到红点吗? These indicate some un-printable characters, that will cause the same error stated above, a corrupted nuget.config这些表示一些不可打印的字符,这将导致上述相同的错误,nuget.config 损坏

在此处输入图片说明

In My Case I have follow the below steps :在我的情况下,我遵循以下步骤:

  1. Repair the Visual studio 2019 using Visual studio installer app.使用 Visual Studio 安装程序应用修复 Visual Studio 2019。

  2. Clear all nugate cashes using Tools ==> Option ==> Nugate Package Manager ==> General ==> Clear All NuGage Cache(s).使用 Tools ==> Option ==> Nugate Package Manager ==> General ==> Clear All NuGage Cache(s) 清除所有 nugate cashes。

  3. Clean Solution and Build the solution.清洁解决方案并构建解决方案。

Following helped me resolving this issue.以下帮助我解决了这个问题。

  1. Clear all nugate cashes using Visual Studio Tools ==> Option ==> Nuget Package Manager ==> General ==> Clear All NuGet Cache(s).使用 Visual Studio Tools ==> Option ==> Nuget Package Manager ==> General ==> Clear All NuGet Cache(s) 清除所有 nugate cashes。
  2. Close Visual Studio.关闭 Visual Studio。
  3. Went to my project in VS Command Prompt where the .sln file is located (ie C:\\Projects\\Project1)在 .sln 文件所在的 VS 命令提示符中转到我的项目(即 C:\\Projects\\Project1)
  4. Executed below command to restore all nuget packages: msbuild -t:restore执行以下命令以恢复所有 nuget 包:msbuild -t:restore
  5. Open the project in Visual Studio and rebuild SOLUTION from VS.在 Visual Studio 中打开项目并从 VS 重建 SOLUTION。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM