Вопрос

I have an XML that looks like this:

   <Weather Location="Tuzla, Bosnia and Herzegovina">
  <Forecast>
     <Description>sky is clear</Description>
     <IconID>01d</IconID>
     <Date>2013-09-14</Date>
     <MinTemp>11</MinTemp>
     <MaxTemp>21</MaxTemp>
     <Pressure>994</Pressure>
     <Humidity>39</Humidity>
     <Wind>2</Wind>
   </Forecast>
   <Forecast>
     <Description>scattered clouds</Description>
     <IconID>03d</IconID>
     <Date>2013-09-15</Date>
     <MinTemp>9</MinTemp>
     <MaxTemp>24</MaxTemp>
     <Pressure>991</Pressure>
     <Humidity>44</Humidity>
     <Wind>1</Wind>
  </Forecast>
  <Forecast>
 ... //it has 10 Forecast parts   
</Weather>

In my database I have table that has just two fields, ID and this XML. So, when I insert new XML, i have to find out if there already exists XML with this location, and where date from first Forecast parts are same. Is there any way I can read only Date from first Forecast child of this root element Weather? I know I have to go through my table and search every XML file I have, and that's not problem, but what I need is just date from only first forecast child.

Please help...

Это было полезно?

Решение

you can read Date from first Forecast and Location like (assuming this is SQL Server)

select
    t.Data.value('(Weather/@Location)[1]', 'nvarchar(max)') as Location,
    t.Data.value('(Weather/Forecast/Date)[1]', 'date') as Date
from table1 as t

sql fiddle demo

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top