Reputation: 137
I have this code to load the xml (to a List) and it works fine:
public MainPage()
{
this.InitializeComponent();
string XMLFilePath = Path.Combine(Package.Current.InstalledLocation.Path, "something.xml");
XDocument loadedData = XDocument.Load(XMLFilePath);
}
what if I want to call the xml from my server!?
This is the last try:
using System.Net.Http;
using System.Runtime.Serialization.Json;
...
private string jsonString;
public MainPage()
{
this.InitializeComponent();
loadData();
//string XMLFilePath = Path.Combine(Package.Current.InstalledLocation.Path, "something.xml");
XDocument loadedData = XDocument.Load(jsonString);
...
private async void loadData()
{
var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://domain.com/something.xml"));
jsonString = await response.Content.ReadAsStringAsync();
}
this is the error:
An exception of type 'System.ArgumentNullException' occurred in System.Xml.ReaderWriter.dll but was not handled in user code
Any help would be great.
Thanks.
Upvotes: 2
Views: 1970
Reputation: 16652
You can simply use XmlDocument.LoadFromUriAsync | loadFromUriAsync methods to asynchronously loads an XML document from the specified location. This location can also be a Http link.
Just for a very simple test here:
<TextBlock x:Name="txt" />
code behind:
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
XmlDocument xmlDoc = await XmlDocument.LoadFromUriAsync(new Uri("http://domain.com/something.xml"));
txt.Text = xmlDoc.GetXml().ToString();
}
Just be noticed that the namespace for xml document here is Windows.Data.Xml.Dom
, not System.Xml
. And I noticed that your Uri address is like http://domain.com/something.xml
, maybe you are using the native server, when doing this, you may probably need to enable the Private Networks(Client & Server)
capability in your project's manifest file.
Update:
Sorry I didn't know you want to use LinQ
, to use XDocument
to load xml, there are basically two methods: from string Uri or from stream, but for the first method, the Uri parameter must be a file system relative or absolute path.
So, here one method is you can save the xml to local file and load it again with XDocument
, or you can use stream to achieve this, for example:
HttpClient client = new HttpClient();
var response = await client.GetStreamAsync("http://domain.com/something.xml");
XDocument xmlDoc = XDocument.Load(response);
txt.Text = xmlDoc.Document.ToString();
Upvotes: 5
Reputation: 4327
Your private async void loadData() is not await and the jsonString is null. You can
private async void loadData()
{
var httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(new Uri("http://domain.com/something.xml"));
jsonString = await response.Content.ReadAsStringAsync();
XDocument loadedData = XDocument.Load(jsonString);
}
public MainPage()
{
this.InitializeComponent();
loadData();
}
Upvotes: 0
Reputation: 1155
You need to specify the address, try something like this:
public MainPage()
{
this.InitializeComponent();
string XMLFilePath = @"http://www.msn.com";
XmlDocument LoadedData = new XmlDocument();
LoadedData.Load(XMLFilePath);
}
If you want to use the XDocument object try to use the method "Parse" instead of "Load".
Upvotes: 1