首页 / XML / c# – 从资源或站点获取XML
c# – 从资源或站点获取XML
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 从资源或站点获取XML,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1993字,纯文字阅读大概需要3分钟。
内容图文
![c# – 从资源或站点获取XML](/upload/InfoBanner/zyjiaocheng/792/0d7b3d8f9f2c46d4beee0c0f26a09658.jpg)
我有两个最后代码行的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);
编辑:
要从Web获取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全部内容,希望文章能够帮你解决c# – 从资源或站点获取XML所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。