2

私のコード:

var myList = xDoc.Descendants("localita").Select(n => new
{
    ID = n.Element("id").Value.ToString(),
    Localita = n.Element("nome").Value.ToString(),
    Lat = n.Element("lat").Value.ToString(),
    Lng = n.Element("lon").Value.ToString(),
    MeteoOggi = new MeteoGiorno()
    {
        Min = n.Descendants("previsione").First().Element("temp_perc").Value.ToString(),
        Max = n.Descendants("previsione").First().Element("temp").Value.ToString(),
        DescrizioneTempo = n.Descendants("previsione").First().Element("desc_tempo").Value.ToString(),
        Precipitazioni = n.Descendants("previsione").First().Element("prec").Value.ToString(),
        VentoDirezione = n.Descendants("previsione").First().Element("v_dir").Value.ToString(),
        VentoIntensita = n.Descendants("previsione").First().Element("v_int").Value.ToString(),
        Pressione = n.Descendants("previsione").First().Element("press").Value.ToString(),
        ZeroTermico = n.Descendants("previsione").First().Element("zerot").Value.ToString(),
        Immagine = n.Descendants("previsione").First().Element("id_tempo").Value.ToString()
    }
});

しかし、ご覧n.Descendants("previsione").First()のとおり、 Class の値を設定するたびに「検索」されますMeteoGiorno。私の例でそのノードへの参照のようなものを入れることはできますか?

4

1 に答える 1

6

確かに、変更するだけですSelect

var myList = xDoc.Descendants("localita").Select(n => {
   var previsione = n.Descendants("previsione").First();

   return new {
      ID = n.Element("id").Value.ToString(),
      ....
      MeteoOggi = new MeteoGiorno()
      {
          Min = previsione.Element("temp_perc").Value.ToString(),
          Max = previsione.Element("temp").Value.ToString(),
          ....
      }
   }
});
于 2012-12-06T14:39:09.077 に答える