1

我有一个如下的 xml 文件

<data>
  <request>
    <type></type>
    <query></query>
  </request>
  <current_condition></current_condition>
  <weather>
    <date>26-12-2012</date>
    <astronomy>
      <sunrise></sunrise>
    </astronomy>
    <maxtempC/>
    <maxtempF/>
    <hourly>
      <time>0</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
    <hourly>
      <time>6</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
    <hourly>
      <time>12</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
    <hourly>
      <time>18</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
  </weather>
  <weather>
    <date>27-12-2012</date>
    <astronomy>
      <sunrise></sunrise>
    </astronomy>
    <maxtempC/>
    <maxtempF/>
    <hourly>
      <time>0</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
    <hourly>
      <time>6</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
    <hourly>
      <time>12</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
    <hourly>
      <time>18</time>
      <tempC>4</tempC>
      <tempF>39</tempF>
      <windspeedMiles>17</windspeedMiles>
    </hourly>
  </weather>
</data>

即使我使用以下内容,我也可以从 current_condition 和天气中获取数据:

var fiveDayForcastDayTwo = _test("washington, tyne and wear").Skip(1).First();将于 27 日返回

var fiveDayForcastDayTwo = _test("washington, tyne and wear").Skip(2).First();将返回 28 日等

我遇到的问题是每小时,我如何遍历每小时节点并获取时间 0 和时间 6 等的数据,同时也跳到第二天。

任何帮助将不胜感激乔治

cs文件的代码

public IEnumerable<DisplayWeatherConditions> DisplayFiveDayForcast(string id)
{
        XDocument doc = XDocument.Load(string.Format("http://www.worldweatheronline.com/feed/premium-weather-v2.ashx?key={0}&feedkey={1}&format=xml&q={2}&tp=6", 
                                       sPartnerID, sLicenseKey, HttpUtility.UrlEncode(id)));

        var displayFiveDayForcast = from wd in doc.Descendants("weather")
                  select new DisplayWeatherConditions()
                  {
                      date          = (string)wd.Element("date") ?? "NA",
                      sunRise       = (string)wd.Element("astronomy").Element("sunrise") ?? "NA",
                      sunSet        = (string)wd.Element("astronomy").Element("sunset") ?? "NA",
                      maxtempC      = (string)wd.Element("maxtempC") ?? "NA",
                      maxtempF      = (string)wd.Element("maxtempF") ?? "NA",
                      mintempC      = (string)wd.Element("mintempC") ?? "NA",
                      mintempF      = (string)wd.Element("mintempF") ?? "NA",
                      hourlyTempC   = (string)wd.Element("hourly")?? "NA",
                  };
        return displayFiveDayForcast.AsEnumerable();
}
4

1 回答 1

0

我能够通过使用以下内容从您提供的(固定)XML 数据中获取您请求的值(请注意,我使用匿名类型来收集值)。

XDocument doc = XDocument.Load([your source document]);
var v1 = doc.Descendants("weather");
var v3 = v1.Where(e => e.Element("date").Value == "26-12-2012");
var v2 = v3.Elements("hourly")
    .Select(e =>
        new
        {
            hour = e.Element("time").Value,
            tempC = e.Element("tempC").Value,
            tempF = e.Element("tempF").Value,
            wind = e.Element("windspeedMiles").Value
        });

或者在您已经拥有的代码的上下文中:

...
hourlyTempC   = wd.Descendants("hourly").Select(e =>
                    new
                    {
                        hour = e.Element("time").Value,
                        tempC = e.Element("tempC").Value,
                        tempF = e.Element("tempF").Value,
                        wind = e.Element("windspeedMiles").Value
                    })
...

您提供的代码将生成的第一个元素hourly转换为字符串 - 但由于您有多个hourly元素,您需要一个可以处理它的结构(如 IEnumerable) - 这正是Select.

于 2012-12-26T21:18:11.393 回答