2014-04-11 104 views
0

我在鑽研使用.SelectMany選擇許多返回

的XML結構是

<data> 
    <request> 
    <type>LatLon</type> 
    <query>Lat 48.85 and Lon 2.35</query> 
    </request> 
    <current_condition> 
    <observation_time>12:38 PM</observation_time> 
    <isdaytime>yes</isdaytime> 
    <temp_C>16</temp_C> 
    <temp_F>61</temp_F> 
    <weatherCode>116</weatherCode> 
    <weatherIconUrl> 
     <![CDATA[ 
     http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png 
     ]]> 
    </weatherIconUrl> 
    <weatherDesc> 
     <![CDATA[ Partly Cloudy ]]> 
    </weatherDesc> 
    <windspeedMiles>7</windspeedMiles> 
    <windspeedKmph>11</windspeedKmph> 
    <winddirDegree>20</winddirDegree> 
    <winddir16Point>NNE</winddir16Point> 
    <precipMM>0.0</precipMM> 
    <humidity>51</humidity> 
    <visibility>10</visibility> 
    <pressure>1018</pressure> 
    <cloudcover>75</cloudcover> 
    <FeelsLikeC>16</FeelsLikeC> 
    <FeelsLikeF>61</FeelsLikeF> 
    </current_condition> 
    <weather> 
    <date>2014-04-11</date> 
    <astronomy> 
     <sunrise>07:08 AM</sunrise> 
     <sunset>08:36 PM</sunset> 
     <moonrise>04:45 PM</moonrise> 
     <moonset>05:15 AM</moonset> 
    </astronomy> 
    </weather> 
<data> 

,我試圖從天文學,但我的成績得到了數據的XML文件的問題沒有結果設置爲空

我的代碼是:

var displayAll = (result.Descendants("data") 
       .Descendants("current_condition") 
       .SelectMany(astronomy => astronomy.Descendants("weather") 
       .Select(wd => new DisplayWeatherCurrentConditions() 
       { 
        data removed for clarity as many lines such as 
PrecipMm = Sanitizer.GetSafeHtmlFragment((string) wd.Element("precipMM")), 

        AstronomyInfo = wd.Elements("astronomy").Select(
         dwa => new DisplayWeatherAstronomy() 
         { 
          SunRise = (string) wd.Element("sunrise") ?? string.Empty, 
          SunSet = (string) wd.Element("sunset") ?? string.Empty 
         } 
         ).ToList() 
       }) 

       )); 


      return displayAll; 

任何幫助,我要去哪裏錯了會讚賞,因爲我已經使用了許多變化,但似乎無法做到正確。

謝謝

---------------------------------代碼編輯------ ---------------------

我設法讓代碼以我想要的方式工作,不確定它的正確方式,但這是我做了什麼。

創建的類,如下構造:

public DisplayWeatherCurrentConditions(DisplayWeatherAstronomy dwa) 
     { 
      SunRise = dwa.SunRise; 
      Sunset = dwa.SunSet; 
     } 
     public string SunRise { get; set; } 
     public string Sunset { get; set; } 

然後改變代碼

var displayAll = (from wd in result.Descendants("current_condition") 
        from ts in result.Descendants("astronomy") 

終於能夠從天文學類

SunRise = (string)ts.Element("sunrise") ?? string.Empty, 
        Sunset = (string)ts.Element("sunset") ?? string.Empty, 

與添加屬性如果有人比我更多的經驗可以改善這個請做

+0

無效的XML塊它沒有完成 – csharpwinphonexaml

+0

請格式化XML閱讀它更好 – csharpwinphonexaml

+1

''沒有後代''。 –

回答

0

只是刪除.Descendants("data")使用result.Descendants("current_condition"),也是我沒有看到任何weather元素的current_condition裏面,如果你想獲得的元素與weather開始,那麼你需要使用

result.Descendants("current_condition") 
     .Descendants() 
     .Where(x => x.Name.ToString().StartsWith("weather")) 

或者,如果你只關心得到的只是weather元素使用result.Descendants("weather")

+0

嗨Selman22謝謝你的回覆,我已經得到了當前的條件返回,但是和天文學一樣掙扎着返回0。試 –