using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.IO;

namespace WPWebClientDataRequest
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;
        }

        private void btnRequestData_Click(object sender, RoutedEventArgs e)
        {
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri("http://services.odata.org/Northwind/Northwind.svc/Customers"));
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            XDocument root = XDocument.Parse(e.Result, LoadOptions.None);
            XNamespace sp = "http://www.w3.org/2005/Atom";
            XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
            XNamespace meta = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

            var items = from item in root.Descendants(sp + "entry")
                        select new Customer()
                        {
                            CustomerID = item
                                .Element(sp + "content")
                                .Element(meta + "properties")
                                .Element(ds + "CustomerID").Value.ToString(),
                            CompanyName = item
                                .Element(sp + "content")
                                .Element(meta + "properties")
                                .Element(ds + "CompanyName").Value.ToString(),
                            ContactName = item
                                .Element(sp + "content")
                                .Element(meta + "properties")
                                .Element(ds + "ContactName").Value.ToString(),
                            ContactTitle = item
                                .Element(sp + "content")
                                .Element(meta + "properties")
                                .Element(ds + "ContactTitle").Value.ToString(),
                            Address = item
                                .Element(sp + "content")
                                .Element(meta + "properties")
                                .Element(ds + "Address").Value.ToString(),
                            City = item
                                .Element(sp + "content")
                                .Element(meta + "properties")
                                .Element(ds + "City").Value.ToString()
                        };
            lbCustomers.ItemsSource = items ;
        }
    }
}