0

WP7 アプリケーションにフォーミュラ 1 API を使用しようとしています。正しいノードと要素を選択する正しい方法が見つかりません。正しい方法は何ですか?これは xml です: ergast.com/api/f1/2012/drivers

DriversPage.xaml.cs

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;

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

        List<Driver> drivers = new List<Driver>();

            drivers = (from element in xmlEntries.Root.Element("DriverTable").Descendants("Driver")
                      select new Driver(element.Attribute("driverId").Value)).ToList<Driver>();

            DriverListBox.ItemsSource = drivers;

    }
  }
}

API

<?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>
                <Driver driverId="button" url="http://en.wikipedia.org/wiki/Jenson_Button">
                        <GivenName>Jenson</GivenName>
                        <FamilyName>Button</FamilyName>
                        <DateOfBirth>1980-01-19</DateOfBirth>
                        <Nationality>British</Nationality>
                </Driver>
                <Driver driverId="rosa" url="http://en.wikipedia.org/wiki/Pedro_de_la_Rosa">
                        <GivenName>Pedro</GivenName>
                        <FamilyName>de la Rosa</FamilyName>
                        <DateOfBirth>1971-02-24</DateOfBirth>
                        <Nationality>Spanish</Nationality>
                </Driver>
                <Driver driverId="resta" url="http://en.wikipedia.org/wiki/Paul_di_Resta">
                        <GivenName>Paul</GivenName>
                        <FamilyName>di Resta</FamilyName>
                        <DateOfBirth>1986-04-16</DateOfBirth>
                        <Nationality>Scottish</Nationality>
                </Driver>
                <Driver driverId="glock" url="http://en.wikipedia.org/wiki/Timo_Glock">
                        <GivenName>Timo</GivenName>
                        <FamilyName>Glock</FamilyName>
                        <DateOfBirth>1982-03-18</DateOfBirth>
                        <Nationality>German</Nationality>
                </Driver>
                <Driver driverId="grosjean" url="http://en.wikipedia.org/wiki/Romain_Grosjean">
                        <GivenName>Romain</GivenName>
                        <FamilyName>Grosjean</FamilyName>
                        <DateOfBirth>1986-04-17</DateOfBirth>
                        <Nationality>French</Nationality>
                </Driver>
                <Driver driverId="hamilton" url="http://en.wikipedia.org/wiki/Lewis_Hamilton">
                        <GivenName>Lewis</GivenName>
                        <FamilyName>Hamilton</FamilyName>
                        <DateOfBirth>1985-01-07</DateOfBirth>
                        <Nationality>British</Nationality>
                </Driver>
                <Driver driverId="hulkenberg" url="http://en.wikipedia.org/wiki/Nico_H%C3%BClkenberg">
                        <GivenName>Nico</GivenName>
                        <FamilyName>Hülkenberg</FamilyName>
                        <DateOfBirth>1987-08-19</DateOfBirth>
                        <Nationality>German</Nationality>
                </Driver>
                <Driver driverId="karthikeyan" url="http://en.wikipedia.org/wiki/Narain_Karthikeyan">
                        <GivenName>Narain</GivenName>
                        <FamilyName>Karthikeyan</FamilyName>
                        <DateOfBirth>1977-01-14</DateOfBirth>
                        <Nationality>Indian</Nationality>
                </Driver>
                <Driver driverId="kobayashi" url="http://en.wikipedia.org/wiki/Kamui_Kobayashi">
                        <GivenName>Kamui</GivenName>
                        <FamilyName>Kobayashi</FamilyName>
                        <DateOfBirth>1986-09-13</DateOfBirth>
                        <Nationality>Japanese</Nationality>
                </Driver>
                <Driver driverId="kovalainen" url="http://en.wikipedia.org/wiki/Heikki_Kovalainen">
                        <GivenName>Heikki</GivenName>
                        <FamilyName>Kovalainen</FamilyName>
                        <DateOfBirth>1981-10-19</DateOfBirth>
                        <Nationality>Finnish</Nationality>
                </Driver>
                <Driver driverId="maldonado" url="http://en.wikipedia.org/wiki/Pastor_Maldonado">
                        <GivenName>Pastor</GivenName>
                        <FamilyName>Maldonado</FamilyName>
                        <DateOfBirth>1985-03-09</DateOfBirth>
                        <Nationality>Venezuelan</Nationality>
                </Driver>
                <Driver driverId="massa" url="http://en.wikipedia.org/wiki/Felipe_Massa">
                        <GivenName>Felipe</GivenName>
                        <FamilyName>Massa</FamilyName>
                        <DateOfBirth>1981-04-25</DateOfBirth>
                        <Nationality>Brazilian</Nationality>
                </Driver>
                <Driver driverId="perez" url="http://en.wikipedia.org/wiki/Sergio_P%C3%A9rez">
                        <GivenName>Sergio</GivenName>
                        <FamilyName>Pérez</FamilyName>
                        <DateOfBirth>1990-01-26</DateOfBirth>
                        <Nationality>Mexican</Nationality>
                </Driver>
                <Driver driverId="petrov" url="http://en.wikipedia.org/wiki/Vitaly_Petrov">
                        <GivenName>Vitaly</GivenName>
                        <FamilyName>Petrov</FamilyName>
                        <DateOfBirth>1984-09-08</DateOfBirth>
                        <Nationality>Russian</Nationality>
                </Driver>
                <Driver driverId="pic" url="http://en.wikipedia.org/wiki/Charles_Pic">
                        <GivenName>Charles</GivenName>
                        <FamilyName>Pic</FamilyName>
                        <DateOfBirth>1990-02-15</DateOfBirth>
                        <Nationality>French</Nationality>
                </Driver>
                <Driver driverId="raikkonen" url="http://en.wikipedia.org/wiki/Kimi_R%C3%A4ikk%C3%B6nen">
                        <GivenName>Kimi</GivenName>
                        <FamilyName>Räikkönen</FamilyName>
                        <DateOfBirth>1979-10-17</DateOfBirth>
                        <Nationality>Finnish</Nationality>
                </Driver>
                <Driver driverId="ricciardo" url="http://en.wikipedia.org/wiki/Daniel_Ricciardo">
                        <GivenName>Daniel</GivenName>
                        <FamilyName>Ricciardo</FamilyName>
                        <DateOfBirth>1989-07-01</DateOfBirth>
                        <Nationality>Australian</Nationality>
                </Driver>
                <Driver driverId="rosberg" url="http://en.wikipedia.org/wiki/Nico_Rosberg">
                        <GivenName>Nico</GivenName>
                        <FamilyName>Rosberg</FamilyName>
                        <DateOfBirth>1985-06-27</DateOfBirth>
                        <Nationality>German</Nationality>
                </Driver>
                <Driver driverId="michael_schumacher" url="http://en.wikipedia.org/wiki/Michael_Schumacher">
                        <GivenName>Michael</GivenName>
                        <FamilyName>Schumacher</FamilyName>
                        <DateOfBirth>1969-01-03</DateOfBirth>
                        <Nationality>German</Nationality>
                </Driver>
                <Driver driverId="bruno_senna" url="http://en.wikipedia.org/wiki/Bruno_Senna">
                        <GivenName>Bruno</GivenName>
                        <FamilyName>Senna</FamilyName>
                        <DateOfBirth>1983-10-15</DateOfBirth>
                        <Nationality>Brazilian</Nationality>
                </Driver>
                <Driver driverId="vergne" url="http://en.wikipedia.org/wiki/Jean-%C3%89ric_Vergne">
                        <GivenName>Jean-Éric</GivenName>
                        <FamilyName>Vergne</FamilyName>
                        <DateOfBirth>1990-04-25</DateOfBirth>
                        <Nationality>French</Nationality>
                </Driver>
                <Driver driverId="vettel" url="http://en.wikipedia.org/wiki/Sebastian_Vettel">
                        <GivenName>Sebastian</GivenName>
                        <FamilyName>Vettel</FamilyName>
                        <DateOfBirth>1987-07-03</DateOfBirth>
                        <Nationality>German</Nationality>
                </Driver>
                <Driver driverId="webber" url="http://en.wikipedia.org/wiki/Mark_Webber">
                        <GivenName>Mark</GivenName>
                        <FamilyName>Webber</FamilyName>
                        <DateOfBirth>1976-08-27</DateOfBirth>
                        <Nationality>Australian</Nationality>
                </Driver>
        </DriverTable>
</MRData>
4

2 に答える 2

2

名前空間が問題を引き起こしていると思います。この方法で試すことができます:

drivers = (from element in xmlEntries.Descendants()
           where element.Name.LocalName == "Driver"
           select new Driver(element.Attribute("driverId").Value)).ToList<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)).ToList<Driver>();
于 2012-05-10T17:13:02.713 に答える
0

これを試して

    private void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            return;
        }        
        XDocument xmlEntries = XDocument.Parse(e.Result); 
        IEnumerable<Driver> list = from item in feed.Descendants("channel").Elements("Driver")
                                       select new Driver
                                       {
                                           Name = (string)item.Attribute("driverId")                                              
                                       };
        ObservableCollection<Driver>() collection = new ObservableCollection<Driver>();
        foreach (Driver d in list)
        {
            collection.Add(d);
        }

        DriverListBox.ItemsSource = collection;
    }
于 2012-05-10T20:27:19.760 に答える