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

2019-09-09 15:34发布

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;
        }
    }

0条回答
登录 后发表回答