2

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.

Rpgccv
  • 137
  • 2
  • 14
  • 1
    what is now working for you edit your question and explain it `It's not Working` does not provide anyone any useful information where does the xml file reside..? – MethodMan Jul 03 '16 at 21:48
  • Is it explicit now @MethodMan ? Thanks – Rpgccv Jul 03 '16 at 22:11

3 Answers3

5

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();
Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • That example works with my xml link, thanks; But i'm using linq, how can I achieve that with XDocument instead!? – Rpgccv Jul 04 '16 at 11:19
  • 1
    You should wrap `HttpClient()` with `using`: `using (var client = new HttpClient()) { /*your code */ }` – aloisdg Jul 05 '16 at 02:01
  • @aloisdg, I can understand you want to dispose this `HttpClient` by warping the code with `using`, but there is no "should". You can refer to [Do HttpClient and HttpClientHandler have to be disposed?](http://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed) – Grace Feng Jul 05 '16 at 02:09
  • @GraceFeng-MSFT I know but here i guessed that OP is going to put it inside a method and call the method each time, creating a new client each time. I put my client outside of the method and don't use using myself for the reason written in this SO post (especially http://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed#comment40535896_15708633). – aloisdg Jul 05 '16 at 12:20
  • I think the httpclient version is the better route. – Brady Moritz Sep 10 '20 at 00:30
1

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".

Dark Templar
  • 1,130
  • 8
  • 10
  • Thanks @Dark, already tried that and the last "XMLFilePath" word throw: "(local variable) string XMLFilePath Argument 1: cannot convert from 'string' to 'System.IO.Stream'" – Rpgccv Jul 03 '16 at 22:15
0

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();
 }
lindexi
  • 4,182
  • 3
  • 19
  • 65