使用File.ReadAllText不支持给定路径的格式

时间:2022-07-20 13:26:47

I have a WPF application. I am loading some data from XML file.

我有一个WPF应用程序。我正在从XML文件加载一些数据。

I receive an error:

我收到一个错误:

System.NotSupportedException was unhandled
Message: The given path's format is not supported.

on this line:

在这条线上:

string html = File.ReadAllText(Advertisement.DescriptionUrl);

the url in the xml it is:

它是xml中的url:

http://mysitetest.com/x/x/Assets/shop/shopdetails/Coffee/image.png

Any ideas how to fix it?

任何想法如何解决它?

2 个解决方案

#1


2  

File.ReadAllText is meant to take a filename on a file system - not a URL.

File.ReadAllText用于在文件系统上获取文件名 - 而不是URL。

You'll need to fetch it with something like WebClient.DownloadString:

您需要使用WebClient.DownloadString之类的东西来获取它:

string text;
using (WebClient client = new WebClient())
{
    text = client.DownloadString(url);
}
// Now use text

#2


1  

That's a web URL, not a file path.

这是一个Web URL,而不是文件路径。

Use a WebClient object to request the resource:

使用WebClient对象来请求资源:

string html;
using (WebClient client = new WebClient()) {
  html = client.DownloadString(Advertisement.DescriptionUrl);
}

#1


2  

File.ReadAllText is meant to take a filename on a file system - not a URL.

File.ReadAllText用于在文件系统上获取文件名 - 而不是URL。

You'll need to fetch it with something like WebClient.DownloadString:

您需要使用WebClient.DownloadString之类的东西来获取它:

string text;
using (WebClient client = new WebClient())
{
    text = client.DownloadString(url);
}
// Now use text

#2


1  

That's a web URL, not a file path.

这是一个Web URL,而不是文件路径。

Use a WebClient object to request the resource:

使用WebClient对象来请求资源:

string html;
using (WebClient client = new WebClient()) {
  html = client.DownloadString(Advertisement.DescriptionUrl);
}