XML循环子数据

时间:2012-12-26 14:02:46

标签: c# xml

我有一个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和weather获取数据,即使我使用以下内容:

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();
}

1 个答案:

答案 0 :(得分: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返回。