简体   繁体   English

System.Xml.XmlException: 给定编码中的字符无效。 第 1 行,位置 1。使用 Nuget 时

[英]System.Xml.XmlException: Invalid character in the given encoding. Line 1, position 1. When using Nuget

For the last 24hrs Nuget seems to be throwing a weird error to me.在过去的 24 小时内,Nuget 似乎向我抛出了一个奇怪的错误。

Everytime I try to install or uninstall a package.. it fails with the following error:每次我尝试安装或卸载软件包时,它都会失败并出现以下错误:

System.Xml.XmlException: Invalid character in the given encoding. Line 1, position 1.

The total stack is:总堆栈为:

Attempting to gather dependencies information for package 'Microsoft.AspNet.WebApi.Client.5.2.3' with respect to project 'User Interface\SMRH.LPIC.Web', targeting '.NETFramework,Version=v4.5.1'
Attempting to resolve dependencies for package 'Microsoft.AspNet.WebApi.Client.5.2.3' with DependencyBehavior 'Lowest'
Resolving actions to install package 'Microsoft.AspNet.WebApi.Client.5.2.3'
Resolved actions to install package 'Microsoft.AspNet.WebApi.Client.5.2.3'
For adding package 'Microsoft.AspNet.WebApi.Client.5.2.3' to project 'SMRH.LPIC.Web' that targets 'net451'.
For adding package 'Microsoft.AspNet.WebApi.Client.5.2.3' to project    'SMRH.LPIC.Web' that targets 'net451'.
Package 'Microsoft.AspNet.WebApi.Client.5.2.3' already exists in folder   'F:\TFS\DevTeamCode\LPIC\SMRH.LPIC\packages'
Added reference 'System.Net.Http.Formatting' to project 'SMRH.LPIC.Web'.
Added package 'Microsoft.AspNet.WebApi.Client.5.2.3' to 'packages.config'
Successfully installed 'Microsoft.AspNet.WebApi.Client 5.2.3' to SMRH.LPIC.Web
Added file 'web.config'.
Install failed. Rolling back...
System.Xml.XmlException: Invalid character in the given encoding. Line 1, position 1.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, Int32& charsCount)
at System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount)
at System.Xml.XmlTextReaderImpl.ReadData()
at System.Xml.XmlTextReaderImpl.SwitchEncoding(Encoding newEncoding)
at System.Xml.XmlTextReaderImpl.SwitchEncodingToUTF8()
at System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl)
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options)
at NuGet.ProjectManagement.XmlUtility.LoadSafe(Stream input, LoadOptions options)
at NuGet.ProjectManagement.XmlUtility.GetDocument(String root, String path)
at NuGet.ProjectManagement.XmlUtility.GetOrCreateDocument(XName rootName, String root, String path, INuGetProjectContext nuGetProjectContext)
at NuGet.PackageManagement.VisualStudio.BindingRedirectManager.GetConfiguration()
at NuGet.PackageManagement.VisualStudio.BindingRedirectManager.AddBindingRedirects(IEnumerable`1 bindingRedirects)
at NuGet.PackageManagement.VisualStudio.RuntimeHelpers.<AddBindingRedirectsAsync>d__3.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.PackageManagement.VisualStudio.RuntimeHelpers.<AddBindingRedirectsAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.PackageManagement.VisualStudio.RuntimeHelpers.<AddBindingRedirectsAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.PackageManagement.VisualStudio.RuntimeHelpers.<AddBindingRedirectsAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.PackageManagement.VisualStudio.VSMSBuildNuGetProjectSystem.<<AddBindingRedirects>b__55_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.VisualStudio.Threading.JoinableTask.CompleteOnCurrentThread()
at NuGet.PackageManagement.VisualStudio.VSMSBuildNuGetProjectSystem.AddBindingRedirects()
at NuGet.ProjectManagement.MSBuildNuGetProject.PostProcessAsync(INuGetProjectContext nuGetProjectContext, CancellationToken token)
at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.PackageManagement.UI.UIActionEngine.<ExecuteActionsAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.PackageManagement.UI.UIActionEngine.<PerformActionAsync>d__3.MoveNext()
========== Finished ==========

It won't give me what file it is failing at.它不会给我它失败的文件。 Deleting the packages folder and restoring it did not work either.删除包文件夹并恢复它也不起作用。

I have checked the web.config and it does not seem to have formatting issues.我检查了 web.config,它似乎没有格式问题。

Rebooted PC and VS but no luck.重新启动 PC 和 VS 但没有运气。

Using Visual Studio 2015 Enterprise, with C# Environment.在 C# 环境中使用 Visual Studio 2015 Enterprise。

I faced exactly the same issue.我遇到了完全相同的问题。 It was related to nuget cache.它与nuget缓存有关。 I cleared the cache and ran dotnet restore command again and it worked.我清除了缓存并再次运行 dotnet restore 命令并且它起作用了。

Command to clear cache: dotnet nuget locals all --clear清除缓存的命令: dotnet nuget locals all --clear

To restore nuget packages: dotnet restore --no-cache还原 nuget 包: dotnet restore --no-cache

Did you open and save the nuget config file in visual studio?您是否在 Visual Studio 中打开并保存了 nuget 配置文件? I think the problem here is with the way Visual Studio encodes text files.我认为这里的问题在于 Visual Studio 编码文本文件的方式。 I've fallen foul of it before.我以前犯过。 The solution is to open the file in a decent text editor like Notepad++ and change the encoding.解决方案是在像 Notepad++ 这样的文本编辑器中打开文件并更改编码。 The answer here should help Visual Studio inserts invalid characters in batch files .这里的答案应该有助于Visual Studio 在批处理文件中插入无效字符

The default location for NuGet's configuration file is %APPDATA%\\NuGet\\NuGet.Config (DOS) or $ENV:APPDATA\\NuGet\\NuGet.Config (PowerShell). NuGet 配置文件的默认位置是 %APPDATA%\\NuGet\\NuGet.Config (DOS) 或 $ENV:APPDATA\\NuGet\\NuGet.Config (PowerShell)。 APPDATA's value is OS, system drive, and current user dependent. APPDATA 的值取决于操作系统、系统驱动器和当前用户。 For example: D:\\Users\\username\\AppData\\Roaming\\NuGet\\NuGet.config.例如:D:\\Users\\username\\AppData\\Roaming\\NuGet\\NuGet.config。

Taken from https://docs.nuget.org/consume/nuget-config-file取自https://docs.nuget.org/consume/nuget-config-file

I found this to be an encoding problem.我发现这是一个编码问题。 If you open the file in visual studio code you will see that the encoding might be UTF-8 with BOM (it should appear on the bottom left corner).如果您在 Visual Studio 代码中打开文件,您将看到编码可能是带有 BOM 的 UTF-8(它应该出现在左下角)。 Some parsers have difficulty reading the BOM when interpreting files.一些解析器在解释文件时难以读取 BOM。

Try saving the file just as UTF-8 Encoding (in vs code this can be done by clicking on the encoding and selecting the option save as).尝试将文件保存为 UTF-8 编码(在 vs 代码中,这可以通过单击编码并选择另存为选项来完成)。

Hope this helps.希望这可以帮助。

I kept getting this error using VS2019 and I was uninstalling and reinstalling NuGets clearing the cache as a workaround as mentioned in the other answer.我一直在使用 VS2019 收到此错误,并且我正在卸载并重新安装 NuGets,以清除缓存作为其他答案中提到的解决方法。

What actually worked for me was doing a Visual Studio repair .实际上对我有用的是进行Visual Studio 修复

After I've done the repair I faced a few more errors regarding Unable to find project information for... but that got cleared by running dotnet nuget locals all --clear command.在我完成修复后,我遇到了更多关于无法找到项目信息的错误......但是通过运行dotnet nuget locals all --clear命令清除了dotnet nuget locals all --clear Now it works like a charm!现在它就像一个魅力!

暂无
暂无

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

相关问题 System.Xml.XmlException:给定编码中的无效字符。 8271行,位置163 - System.Xml.XmlException: Invalid character in the given encoding. Line 8271, position 163 System.Xml.XmlException:给定编码中的无效字符 - System.Xml.XmlException: Invalid character in the given encoding 如何防止System.Xml.XmlException:给定编码中的字符无效 - How to prevent System.Xml.XmlException: Invalid character in the given encoding System.Xml.XmlException '根级别的数据无效,第 1 行,position 1' 当我从 1 Z0F635D0E0F3874FFF8B581C1532E6C7A7Z 文件更改时出现错误 - System.Xml.XmlException 'Data at the root level is invalid, line 1, position 1' error is showing up when i changed from 1 xml file to 5 System.Xml.XmlException:根级别的数据无效。 第 1 行,position 1 - 有效的 XML 文件? - System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1 - Valid XML file? 获取System.Xml.XmlException:名称不能以&#39;&#39;字符开头,十六进制值为0x20。 第42行,位置36 - Getting System.Xml.XmlException: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 42, position 36 XmlException给定编码中的无效字符 - XmlException Invalid character in the given encoding XmlException:给定编码中的无效字符 - XmlException: Invalid character in the given encoding System.Xml.XmlException:引用未声明的实体“AMP”。 88行,position 68 - System.Xml.XmlException: Reference to undeclared entity 'AMP'. Line 88, position 68 使用LINQ to XML时出现System.Xml.XmlException错误 - System.Xml.XmlException error upon using LINQ to XML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM