我有一个如下的 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();
}