Select XML Elements and attributes

enrociano

New Member
I'm making a formula 1 app for WP7. I used an API to retrieve information. I managed to retrieve the attributes of the driver element but I can't retrieve the elements under it. I used this API: http://ergast.com/api/f1/2012/driversC# \[code\]namespace Formule1{public partial class DriversPage : PhoneApplicationPage{ const string URL = "http://ergast.com/api/f1/2012/"; public DriversPage() { InitializeComponent(); GetDrivers(); } private void GetDrivers() { string resource = "drivers"; WebClient webclient = new WebClient(); webclient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webclient_DownloadStringCompleted); webclient.DownloadStringAsync(new Uri(URL + resource)); } private void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { return; } // xml-resultaat parsen XDocument xmlEntries = XDocument.Parse(e.Result); // drivers eruit halen List<Driver> drivers = new List<Driver>(); var ns = xmlEntries.Root.Name.Namespace; drivers = (from element in xmlEntries.Root.Element(ns + "DriverTable").Descendants(ns + "Driver") select new Driver(element.Attribute("driverId").Value, element.Element("GivenName").Value)).ToList<Driver>(); DriverListBox.ItemsSource = drivers; }}}\[/code\]API\[code\]<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="http://ergast.com/schemas/mrd-1.2.xsl"?><MRData xmlns="http://ergast.com/mrd/1.2" series="f1" url="http://ergast.com/api/f1/2012/drivers" limit="30" offset="0" total="24"><DriverTable season="2012"> <Driver driverId="alonso" url="http://en.wikipedia.org/wiki/Fernando_Alonso"> <GivenName>Fernando</GivenName> <FamilyName>Alonso</FamilyName> <DateOfBirth>1981-07-29</DateOfBirth> <Nationality>Spanish</Nationality> </Driver> </DriverTable></MRData>\[/code\]
 
Back
Top