2009-08-26 15 views
0

私は特定の条件でLINQを使用してXML要素を読んで助けを必要とする場所で私を助けてVb.net-でLINQを使用してノード。読書XML要素&子供が条件:(

私は最大air_tempを見つける必要があります

私は、以下のようなものを試してみましたが、運:郡 すなわち郡名= "ブーン" と時間のID = "00CDT 2009年6月3日09" のための:

Dim custs As IEnumerable = From c In Element.Load("C:\meridian.xml").Elements("county") _ 
          Select c.Elements("hour").Elements("air_temp").Max() 

For Each x In custs 
     Response.Write(custs(0).ToString()) 
Next 

--- ----------------ここにxmlファイル:

XMLリテラルを使用して
<forecasts> 
     <issued>06/02/2009 12:00CDT</issued> 
    - <county name="Adair"> 
    - <hour id="06/02/2009 12:00CDT"> 
      <air_temp>61</air_temp> 
      <cloud_cover>overcast</cloud_cover> 
      <dew_point>59</dew_point> 
      <precip_prob>90</precip_prob> 
      <precip_rate>0.12</precip_rate> 
      <precip_type>rain</precip_type> 
      <snow_rate>0.0</snow_rate> 
      <wind_direction>NE</wind_direction> 
      <wind_speed>12</wind_speed> 
      <dew_point_confidence>-3/+3</dew_point_confidence> 
      <road_temp>64</road_temp> 
      <road_frost_prob>0</road_frost_prob> 
      <road_potential_evap_rate>429</road_potential_evap_rate> 
      <road_temp_confidence>-3/+2</road_temp_confidence> 
      <dew_point_confidence>-3/+3</dew_point_confidence> 
      <bridge_temp>63</bridge_temp> 
      <bridge_temp_confidence>-4/+2</bridge_temp_confidence> 
      <bridge_frost>NO</bridge_frost> 
     </hour> 
    - <hour id="06/02/2009 13:00CDT"> 
      <air_temp>61</air_temp> 
      <cloud_cover>overcast</cloud_cover> 
      <dew_point>60</dew_point> 
      <precip_prob>70</precip_prob> 
      <precip_rate>0.01</precip_rate> 
      <precip_type>rain</precip_type> 
      <snow_rate>0.0</snow_rate> 
      <wind_direction>ENE</wind_direction> 
      <wind_speed>10</wind_speed> 
      <dew_point_confidence>-3/+3</dew_point_confidence> 
      <road_temp>65</road_temp> 
      <road_frost_prob>0</road_frost_prob> 
      <road_potential_evap_rate>411</road_potential_evap_rate> 
      <road_temp_confidence>-3/+2</road_temp_confidence> 
      <dew_point_confidence>-3/+3</dew_point_confidence> 
      <bridge_temp>64</bridge_temp> 
      <bridge_temp_confidence>-4/+1</bridge_temp_confidence> 
      <bridge_frost>NO</bridge_frost> 
     </hour> 
+2

巨大なXML全体を貼り付ける必要はありません。スニペットを貼り付けるだけで、その仕組みがわかります。 1000行のxmlファイルをトラックする必要がある場合、人々はあまり役に立たないでしょう... –

答えて

1
Dim document = XDocument.Load("meridian.xml") 
Dim air_temps = From county In document.Root.Elements("county") _ 
       From hour in county.Elements("hour") _ 
       From air_temp in hour.Elements("air_temp") _ 
       Select CInt(air_temp) 
Dim max_air_temp = air_temps.Max() 

Dim forecasts = XElement.Load("meridian.xml") 
Dim air_temps = From air_temp In forecasts.<county>.<hour>.<air_temp> _ 
       Select CInt(air_temp) 
Dim max_air_temp = air_temps.Max() 
1

を私はあなたが「国/最大温度が必要であることを前提としています。日付/最大温度。値は、」もしそうなら、これはトリックを行います出力として集計:

Dim meridian = XDocument.Load("meridian.xml") 
Dim maxByCounty = _ 
    From county In meridian.<forecasts>.<county> _ 
    Let maxHour = (From hour In county.<hour> _ 
        Order By CType(hour.<air_temp>, Integer) Desescending _ 
       ).First _ 
    Select New With { .Name = county.<name>, _ 
         .Hour = [email protected], 
         .AirTemp = maxHour.<air_temp> } 

そして、あなたは、このように結果を使用することができます。

For Each m In maxByCounty 
    Console.WriteLine(m.Name, m.Hour, m.AirTemp) 
Next 
1

はそれが

From hour In county.Elements("hour") Where hour.Attribute("id")... 
すべきではありません

代わりの

From hour In county.Elements("hour") Where county.Element("hour").Attribute("id")... 

また、追加の回答として他の投稿への返信を投稿しないでください。