How to parse xml data using c# via an UWP application

妖精的绣舞 提交于 2019-12-25 04:36:22

问题


can anyone please help me with to parse data from xml file(bot a local file in my pc ) . I really don't know what to do ..I think i should use httpclient since it's not a local file...i know i should've put some code snippets but it's really so messed up .. as i changed in i a lot .. thanks in advance First this the code i tried to use but it didn't work

  private async void Start_Click(object sender, RoutedEventArgs e)
    {
        Uri resourceAddress;

        // The value of 'AddressField' is set by the user and is therefore untrusted input. If we can't create a
        // valid, absolute URI, we'll notify the user about the incorrect input.
        if (!Helpers.TryGetUri(AddressField.Text, out resourceAddress))
        {
            rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage);
            return;
        }

        Helpers.ScenarioStarted(StartButton, CancelButton, OutputField);
        rootPage.NotifyUser("In progress", NotifyType.StatusMessage);

        try
        {
            HttpResponseMessage response = await httpClient.GetAsync(resourceAddress).AsTask(cts.Token);

            await Helpers.DisplayTextResultAsync(response, OutputField, cts.Token);
            response.EnsureSuccessStatusCode();

            XElement element = XElement.Parse(await response.Content.ReadAsStringAsync().AsTask(cts.Token));
            OutputList.ItemsSource = (
                from c in element.Elements("item")
                select c.Attribute("name").Value);

            rootPage.NotifyUser("Completed", NotifyType.StatusMessage);
        }
        catch (TaskCanceledException)
        {
            rootPage.NotifyUser("Request canceled.", NotifyType.ErrorMessage);
        }
        catch (Exception ex)
        {
            rootPage.NotifyUser("Error: " + ex.Message, NotifyType.ErrorMessage);
        }
        finally
        {
            Helpers.ScenarioCompleted(StartButton, CancelButton);
        }
    }

    private void Cancel_Click(object sender, RoutedEventArgs e)
    {
        cts.Cancel();
        cts.Dispose();

        // Re-create the CancellationTokenSource.
        cts = new CancellationTokenSource();
    }

    public void Dispose()
    {
        if (httpClient != null)
        {
            httpClient.Dispose();
            httpClient = null;
        }

        if (cts != null)
        {
            cts.Dispose();
            cts = null;
        }
    }

来源:https://stackoverflow.com/questions/36202911/how-to-parse-xml-data-using-c-sharp-via-an-uwp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!