DarthVegan
DarthVegan

Reputation: 1269

How to take an xml response received from a web service and make it a datasource

I'm making a windows 8.1 phone app and I have a listbox that I'm trying to populate with data. My problem is that I am using a web service and I finally figured out how to get the data but it is in an xml format and I don't know how to make that into the itemssource for the listbox. I'm sorry if this is a stupid question but this is my first time doing this and I can't seem to find any information online.

Upvotes: 0

Views: 52

Answers (2)

Ken Tucker
Ken Tucker

Reputation: 4156

If the data coming back is always the same type you can always use XmlSerializer to deserialize the data the data to a class you can bind to. If you copy a sample of the data coming back to the clip board you can use paste xml as classes to paste the class you can deserialize into

Deserialize XML in a WP8 Application

Upvotes: 1

Kulasangar
Kulasangar

Reputation: 9444

Below is a very simple example which requests an XML document from a URI over HTTPS.

It downloads the XML asynchronously as a string and then uses XDocument.Parse() to load it.

private void button2_Click(object sender, RoutedEventArgs e)
{
    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += HttpsCompleted;
    wc.DownloadStringAsync(new Uri("https://domain/path/file.xml"));
}

private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);

        this.textBox1.Text = xdoc.FirstNode.ToString();
    }
}

Have a look over this thread.

Hope it helps!

Upvotes: 1

Related Questions