xmlexception:给定编码中的无效字符.1号线,1号位置.使用Nuget时

本文关键字:1号 1号线 位置 使用 Nuget 字符 无效 编码 xmlexception | 更新日期: 2023-09-27 18:18:56

在过去的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 ==========

它不会告诉我它在哪个文件失败。删除packages文件夹并恢复它也不工作。

我在网上查过了。配置,它似乎没有格式问题。

重启PC和VS,但运气不好。

使用Visual Studio 2015 Enterprise,使用c#环境。

xmlexception:给定编码中的无效字符.1号线,1号位置.使用Nuget时

我也面临同样的问题。这与核缓存有关。我清除了缓存并再次运行dotnet恢复命令,它工作了。

清除缓存:dotnet nuget locals all——clear

dotnet restore——no-cache

我一直使用VS2019得到这个错误,我正在卸载和重新安装NuGets清理缓存作为另一个答案中提到的解决方案。

真正对我起作用的是做一个Visual Studio修复

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

后来编辑:

对于NuGet.targets

中的错误

NuGet。目标错误:给定编码中的无效字符。第一行,位置1

修复是安装最新的dotnet SDK版本。

您是否在visual studio中打开并保存了nuget配置文件?我认为这里的问题在于Visual Studio编码文本文件的方式。我以前也犯过这样的错误。解决方案是在一个体面的文本编辑器(如notepad++)中打开文件并更改编码。这里的答案应该有所帮助Visual Studio在批处理文件中插入无效字符。

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

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

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

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