2011-04-12 57 views
1

嗨即時嘗試使我的第一個Windows Phone 7應用程序。它涉及向服務器查詢航班信息。然後接收一個XML文檔。然後我想創建一系列基於我回來的XML的對象。但是,由於對象值爲空白,因此存在問題。Windows手機7閱讀XML與XDocument

我的代碼

private void SearchButton_Click(object sender, RoutedEventArgs e) 
    { 
     getResults("http://test.com/"); 
    } 

    public void getResults(string websiteURL) 
    { 
     WebClient c = new WebClient(); 
     c.DownloadStringAsync(new Uri(websiteURL)); 
     c.DownloadStringCompleted += new DownloadStringCompletedEventHandler(c_DownloadStringCompleted); 
    } 

    void c_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) 
    { 
     lock (this) 
     { 
      string s = e.Result; 
      XmlReader r = XmlReader.Create(new MemoryStream(System.Text.UnicodeEncoding.Unicode.GetBytes(s))); 
      // So something with the XML we get back 
      XDocument data = XDocument.Load(r); 
      var ns = data.Root.GetDefaultNamespace(); 
      var flights = from query in data.Descendants(ns+"Flight") 
           select new Flight 
           { 
            AircraftType = (int)query.Element(ns + "AircraftType"), 
            ArrivalTerminal = (int)query.Element(ns + "ArrivalTerminal"), 
            Carrier = (string)query.Element(ns + "Carrier"), 
            DepartureTerminal = (int)query.Element(ns + "DepartureTerminal"), 
            Duration = (string)query.Element(ns + "Duration"), 
            EndDateTime = (string)query.Element(ns + "EndDateTime"), 
            EndPoint = (string)query.Element(ns + "EndPoint"), 
            FlightIndexNo = (int)query.Element(ns + "FlightIndexNo"), 
            FlightNo = (int)query.Element(ns + "FlightNo"), 
            NumStops = (int)query.Element(ns + "NumStops"), 
            OperatedBy = (string)query.Element(ns + "OperatedBy"), 
            StartDateTime = (string)query.Element(ns + "StartDateTime"), 
            StartPoint = (string)query.Element(ns + "StartPoint") 
           }; 
      //checking if anything is there. 
      string result =""; 

      foreach (Flight i in flights) 
      { 
       result += i.Carrier; 
      } 
      resultsBlock.Text = result; 


     } 
    } 

    public class Flight 
    { 
     public int aircraftType; 
     public int arrivalTerminal; 
     public string carrier; 
     public int departureTerminal; 
     public string duration; 
     public string endDateTime; 
     public string endPoint; 
     public int flightIndexNo; 
     public int flightNo; 
     public int numStops; 
     public string operatedBy; 
     public string startDateTime; 
     public string startPoint; 

     //Getter and setters 

,我想了XML的部分看起來是這樣的。這也是XML中Flight標籤的第一次使用。增加XML整個事情的開始是數百行,所以不會但它全部結束。我想要的飛行標籤在那裏被磨壞。

<FindFlightsResponse xmlns="urn:webjet.com.au" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
<DisplayMessage i:nil="true" /> 
<OutboundFlightInfo> 
      ... 
       <Flight> 
        <AircraftType>734</AircraftType> 
        <ArrivalTerminal>3</ArrivalTerminal> 
        <Carrier>QF</Carrier> 
        <DepartureTerminal>1</DepartureTerminal> 
        <Duration>PT1H25M</Duration> 
        <EndDateTime>2011-04-20T07:25:00</EndDateTime> 
        <EndPoint>SYD</EndPoint> 
        <FlightIndexNo>1</FlightIndexNo> 
        <FlightNo>400</FlightNo> 
        <NumStops>0</NumStops> 
        <OperatedBy>QF</OperatedBy> 
        <StartDateTime>2011-04-20T06:00:00</StartDateTime> 
        <StartPoint>MEL</StartPoint> 
       </Flight> 

我可能犯了一些簡單的錯誤。 所有幫助表示讚賞。 謝謝。 編輯即時獲取方法傳遞錯誤。爲簡單起見,我將日期時間更改爲字符串

回答

0

我的猜測是您沒有向我們展示完整的XML,並且它可能具有指定的默認名稱空間。命名空間看起來像xmlns="http://www.domain.com"。如果這確實是這種情況,那麼你的LINQ to XML查詢需要引用命名空間,這可以如下進行:

var ns = data.Root.GetDefaultNamespace(); 
var flights = from query in data.Descendants(ns + "Flight") 
       select new Flight 
       { 
        AircraftType = (int)query.Element(ns + "AircraftType"), 
        ArrivalTerminal = (int)query.Element(ns + "ArrivalTerminal"), 
        Carrier = (string)query.Element(ns + "Carrier"), 
        DepartureTerminal = (int)query.Element(ns + "DepartureTerminal"), 
        Duration = (string)query.Element(ns + "Duration"), 
        EndDateTime = (DateTime)query.Element(ns + "EndDateTime"), 
        EndPoint = (string)query.Element(ns + "EndPoint"), 
        FlightIndexNo = (int)query.Element(ns + "FlightIndexNo"), 
        FlightNo = (int)query.Element(ns + "FlightNo"), 
        NumStops = (int)query.Element(ns + "NumStops"), 
        OperatedBy = (string)query.Element(ns + "OperatedBy"), 
        StartDateTime = (DateTime)query.Element(ns + "StartDateTime"), 
        StartPoint = (string)query.Element(ns + "StartPoint") 
       }; 

請注意,您需要與命名空間前綴的每一個元素的名稱,因此使用的整個代碼中都包含ns +

+0

謝謝艾哈邁德你是對的有一個名稱空間指定。但是,我做了更改,仍然存在相同的問題 – user704314 2011-04-12 16:14:56

+0

@ user704314您是否可以使用所做的更改更新您的問題,並使用命名空間顯示更多XML? – 2011-04-12 16:21:35

+0

爲了簡單起見,我更新了代碼並更改了日期時間參數,因爲現在我得到一個錯誤,說我在創建新的Flight時未能處理FormatException。 – user704314 2011-04-13 08:09:07