使用T-SQL解析xml ....我做错了什么

时间:2016-06-15 12:14:24

标签: sql-server xml tsql

我正在尝试解析从webservice获取的xml信息,以后再放入表中。 我没有错误只是一个空的领域,所以我可能会忽略一些小的东西,任何人都可以把我推向正确的方向吗?

Declare @myXml as xml;

set @myXml = '<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://www.webserviceX.NET">
   <CurrentWeather>
      <Location>Eindhoven, Netherlands (EHEH) 51-27N 005-25E 28M</Location>
      <Time>Jun 15, 2016 - 06:55 AM EDT / 2016.06.15 1055 UTC</Time>
      <Wind>from the WSW (240 degrees) at 2 MPH (2 KT):0</Wind>
      <Visibility>greater than 7 mile(s):0</Visibility>
      <SkyConditions>mostly cloudy</SkyConditions>
      <Temperature>62 F (17 C)</Temperature>
      <DewPoint>57 F (14 C)</DewPoint>
      <RelativeHumidity>82%</RelativeHumidity>
      <Pressure>29.50 in. Hg (0999 hPa)</Pressure>
      <Status>Success</Status>
   </CurrentWeather>
</string>';

 SELECT 
   b.value('(./CurrentWeather/Location/text())[1]','Varchar(250)') as [Location] 
FROM @myXml.nodes('/string') as a(b);

1 个答案:

答案 0 :(得分:3)

T-SQL要求您在任何存在时指定名称空间:

with xmlnamespaces(default 'http://www.webserviceX.NET')
select b.value('(./CurrentWeather/Location/text())[1]','Varchar(250)') as [Location]
FROM @myXml.nodes('/string') as a(b);