XML 在 Blazor 中... Wasm:[System.PlatformNotSupportedException] 此平台不支持操作

XML in Blazor... Wasm: [System.PlatformNotSupportedException] Operation is not supported on this platform

我正在尝试在 Blazor 中做一些 XML 的事情,但我只能得到 2 条错误消息。 我尝试的第一件事(使用本地 XML 文件)甚至找不到它...

string xmlPath = "C:\Users\me\Desktop\users.xml";
XElement contacts = XElement.Load(xmlPath);
Console.WriteLine(contacts);

这给出了一个错误,说 WASM:

[System.IO.FileNotFoundException] Could not find file "/C:\Users\me\Desktop\users.xml"

它在路径的开头添加了一个 /。

或者如果我使用随机在线 XML,我会收到 'not supported' 错误。例如...

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("https://www.w3schools.com/xml/note.xml");

Wasm 中显示的完整错误:

[System.PlatformNotSupportedException] Operation is not supported on this platform.

是否只是 Web Assembly 不支持 XML 类 或类似的东西?

Web Assembly 以及 JavaScript 无法访问驻留在用户浏览器上的本地文件。当您尝试访问本地文件时,Web Assembly 会显示一条通知消息,告诉用户它找不到该文件。这是这种情况下的标准程序。

至于第二个错误:您尝试做的是从 Blazor 应用程序向驻留在不同域的服务器发出 http 请求,这需要您配置 CORS,但这不是主要问题. XmlDocument 无法使用 HttpClient 调用 http 请求。我不确定它是否可以创建自己的 HttpClient 实例,如果可以,则应使用与 Blazor 定义的默认 HttpCient 设置相同的设置配置此 HttpClient...

希望这对您有所帮助...