Get XML from resources or site

I have some problem with the URI in the last two lines of code: I'm trying to get XML from application resources and from the site. Before that, I performed the same operations with the image - everything worked fine. Post exception in the comments.

// 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 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 gets the URI parameter as a parameter. Why can't I do this using XML?

+4
source share
1 answer

Judging by the error description, I doubt very much whether it is possible to pass the WPF package URI to XElement.Load , but you can always use the relative path and it will work, the code example below:

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

EDIT:

To get an XML file from the Internet:

 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); } } 
+4
source

Source: https://habr.com/ru/post/1442901/


All Articles