选择多个返回没有结果

时间:2014-04-11 13:08:29

标签: c# xml linq

我在使用.SelectMany

挖掘xml文件时遇到问题

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>

我正试图从天文学中获取数据但我的结果集是空的

我的代码是:

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;

任何有关我出错的帮助都会受到赞赏,因为我使用过很多版本,但似乎无法正确使用。

由于

--------------------------------- Code Edit ------------ ---------------

我设法让代码以我想要的方式工作,不确定它是否正确,但这就是我所做的。

为类创建一个构造函数,如下所示:

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,

如果有比我更多经验的人可以改善这一点,请

1 个答案:

答案 0 :(得分:0)

如果您想要获取以{开头的元素',请删除.Descendants("data")使用result.Descendants("current_condition")并且我在weather内看不到任何current_condition元素{1}}然后你需要使用

weather

或者,如果您只想使用result.Descendants("current_condition") .Descendants() .Where(x => x.Name.ToString().StartsWith("weather"))

获取weather个元素