繁体   English   中英

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

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

在过去的 24 小时内,Nuget 似乎向我抛出了一个奇怪的错误。

每次我尝试安装或卸载软件包时,它都会失败并出现以下错误:

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

总堆栈为:

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

它不会给我它失败的文件。 删除包文件夹并恢复它也不起作用。

我检查了 web.config,它似乎没有格式问题。

重新启动 PC 和 VS 但没有运气。

在 C# 环境中使用 Visual Studio 2015 Enterprise。

我遇到了完全相同的问题。 它与nuget缓存有关。 我清除了缓存并再次运行 dotnet restore 命令并且它起作用了。

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

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

您是否在 Visual Studio 中打开并保存了 nuget 配置文件? 我认为这里的问题在于 Visual Studio 编码文本文件的方式。 我以前犯过。 解决方案是在像 Notepad++ 这样的文本编辑器中打开文件并更改编码。 这里的答案应该有助于Visual Studio 在批处理文件中插入无效字符

NuGet 配置文件的默认位置是 %APPDATA%\\NuGet\\NuGet.Config (DOS) 或 $ENV:APPDATA\\NuGet\\NuGet.Config (PowerShell)。 APPDATA 的值取决于操作系统、系统驱动器和当前用户。 例如:D:\\Users\\username\\AppData\\Roaming\\NuGet\\NuGet.config。

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

我发现这是一个编码问题。 如果您在 Visual Studio 代码中打开文件,您将看到编码可能是带有 BOM 的 UTF-8(它应该出现在左下角)。 一些解析器在解释文件时难以读取 BOM。

尝试将文件保存为 UTF-8 编码(在 vs 代码中,这可以通过单击编码并选择另存为选项来完成)。

希望这可以帮助。

我一直在使用 VS2019 收到此错误,并且我正在卸载并重新安装 NuGets,以清除缓存作为其他答案中提到的解决方法。

实际上对我有用的是进行Visual Studio 修复

在我完成修复后,我遇到了更多关于无法找到项目信息的错误......但是通过运行dotnet nuget locals all --clear命令清除了dotnet nuget locals all --clear 现在它就像一个魅力!

暂无
暂无

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

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