小编Rpg*_*ccv的帖子

UWP - 如何从 http 获取文件 (xml)?

我有这个代码来加载 xml(到列表),它工作正常:

public MainPage()
{
    this.InitializeComponent();
    string XMLFilePath = Path.Combine(Package.Current.InstalledLocation.Path, "something.xml");
    XDocument loadedData = XDocument.Load(XMLFilePath);
}
Run Code Online (Sandbox Code Playgroud)

如果我想从我的服务器调用 xml 怎么办!?

这是最后一次尝试:

using System.Net.Http;
using System.Runtime.Serialization.Json;
Run Code Online (Sandbox Code Playgroud)

...

private string jsonString;

    public MainPage()
    {
        this.InitializeComponent();

        loadData();

        //string XMLFilePath = Path.Combine(Package.Current.InstalledLocation.Path, "something.xml");
        XDocument loadedData = XDocument.Load(jsonString);
Run Code Online (Sandbox Code Playgroud)

...

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();
    }
Run Code Online (Sandbox Code Playgroud)

这是错误:

An exception of type 'System.ArgumentNullException' occurred in System.Xml.ReaderWriter.dll but was not handled in user code …
Run Code Online (Sandbox Code Playgroud)

c# uwp

2
推荐指数
1
解决办法
1742
查看次数

标签 统计

c# ×1

uwp ×1