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