1

我编写了一个 Web 服务和一个 Web 方法,它以 xml 格式返回我的类(DOVIZ_RETURN)的列表,如下所示:

 <?xml version="1.0" encoding="utf-8"?>
<ArrayOfDOVIZ_RETURN xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
    <DOVIZ_RETURN>
        <dovizAdi>AMERİKAN DOLARI</dovizAdi>
        <dovizKodu>USD</dovizKodu>
        <dovizAlis>1.7928</dovizAlis>
        <dovizSatis>1.8014</dovizSatis>
    </DOVIZ_RETURN>
    <DOVIZ_RETURN>
        <dovizAdi>KANADA DOLARI</dovizAdi>
        <dovizKodu>CAD</dovizKodu>
        <dovizAlis>1.8321</dovizAlis>
        <dovizSatis>1.8404</dovizSatis>
    </DOVIZ_RETURN>
</ArrayOfDOVIZ_RETURN>

我正在使用存储过程成功调用此服务。我得到这个xml。但是当我用下面的 sql 解析这个 xml 时,它返回任何行只返回列标题;请看下面:

ALTER PROCEDURE GET_DOVIZ_KUR_RESULT_FROM_SERVIS

AS
    declare @obj int
    declare @servisUrl varchar(200)
    declare @response xml

    SET @servisUrl='http://localhost/DovizKurTestApp/DovizKurService.asmx/GET_DOVIZ_KUR'

    EXEC sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
    EXEC sp_OAMethod @obj,'Open',NULL,'GET',@servisUrl,false
    EXEC sp_OAMethod @obj,'send'
    EXEC sp_OAGetProperty @obj, 'responseText', @response OUT

    create table #DOVIZ_KUR (dovizKurList xml)
    insert into #DOVIZ_KUR select @response 

    SELECT
    a.b.value('dovizAdi[1]', 'Varchar(100)') DovizAdi,
    a.b.value('dovizKodu[1]', 'Varchar(100)') DovizKodu,
    a.b.value('dovizAlis[1]', 'Varchar(100)') DovizAlis,
    a.b.value('dovizSatis[1]', 'Varchar(100)') DovizSatis
    FROM #DOVIZ_KUR
    CROSS APPLY dovizKurList.nodes('/ArrayOfDOVIZ_RETURN/DOVIZ_RETURN') AS a(b)

    EXEC sp_OADestroy @obj

RETURN

这里有什么问题?

4

1 回答 1

2

这是因为名称空间包含在 XML 中。

你可以试试这个

SELECT  
    a.b.value('declare namespace MI="http://tempuri.org/"; MI:dovizAdi[1]', 'Varchar(100)') DovizAdi, 
    a.b.value('declare namespace MI="http://tempuri.org/"; MI:dovizKodu[1]', 'Varchar(100)') DovizKodu, 
    a.b.value('declare namespace MI="http://tempuri.org/"; MI:dovizAlis[1]', 'Varchar(100)') DovizAlis, 
    a.b.value('declare namespace MI="http://tempuri.org/"; MI:dovizSatis[1]', 'Varchar(100)') DovizSatis 
FROM @DOVIZ_KUR d
    CROSS APPLY dovizKurList.nodes('
    declare namespace MI="http://tempuri.org/";
    /MI:ArrayOfDOVIZ_RETURN/MI:DOVIZ_RETURN') AS a(b)

或者甚至更好

;WITH XMLNAMESPACES('http://tempuri.org/' as MI)
SELECT  
    a.b.value('MI:dovizAdi[1]', 'Varchar(100)') DovizAdi, 
    a.b.value('MI:dovizKodu[1]', 'Varchar(100)') DovizKodu, 
    a.b.value('MI:dovizAlis[1]', 'Varchar(100)') DovizAlis, 
    a.b.value('MI:dovizSatis[1]', 'Varchar(100)') DovizSatis 
FROM @DOVIZ_KUR d
    CROSS APPLY dovizKurList.nodes('/MI:ArrayOfDOVIZ_RETURN/MI:DOVIZ_RETURN') AS a(b)

SQL 小提琴演示

于 2012-09-20T15:57:40.487 回答