gpt4 book ai didi

c# - 从资源或站点获取 XML

转载 作者:太空狗 更新时间:2023-10-30 01:22:38 24 4
gpt4 key购买 nike

我在最后两行代码中遇到 URI 问题:我尝试从应用程序资源和站点获取 XML。在此之前,我对图像执行了相同的操作——一切正常。评论中的异常消息。

// Get image from site: 
// "pack://siteoforigin:,,,/http://www.designdownloader.com/item/pngs/user_f036/user_f036-20111114102144-00003.png"

// Get image from building resources (Build Action = Resources).
Uri uri_male_default = new Uri("pack://application:,,,/male.png");

// Get image from site:
// "pack://siteoforigin:,,,/http://www.designdownloader.com/item/pngl/user_f046/user_f046-20111114102341-00003.png"

// Get image from building resources (Build Action = Resources).
Uri uri_female_default = new Uri("pack://application:,,,/myImages/famale.png");

// Create images (it's works fine):
img_male_default = new BitmapImage(uri_male_default);
img_female_default = new BitmapImage(uri_female_default);

//The next both cases ain't working:
// NotSupportedException: URI prefix isn't recognized.
XElement xml_1 = XElement.Load("pack://application:,,,/SettingsX.xml"); // Get XML from building resources (Build Action = Resources).
XElement xml_2 = XElement.Load("pack://siteoforigin:,,,/https://skydrive.live.com/?cid=51b3145b64e05fef&id=51B3145B64E05FEF%21550");

XElement.Load 获取一个 URI 作为参数。为什么我不能用 XML 来做?

最佳答案

根据错误描述判断,我非常怀疑是否可以将 WPF 包 URI 传递给 XElement.Load,但您始终可以使用相对路径并且它会起作用,示例代码如下:

Uri uri = new Uri("/SettingsX.xml", UriKind.Relative);
System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(uri);
XElement settings = XElement.Load(info.Stream);

编辑:

从网络获取xml文件:

private void button1_Click(object sender, RoutedEventArgs e)
{
string url = "Your URL...";
var webClient = new System.Net.WebClient();
webClient.DownloadStringCompleted += HttpsCompleted;
webClient.DownloadStringAsync(new Uri(url));
}

private void HttpsCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
}
}

关于c# - 从资源或站点获取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13132564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com